Java Reference
In-Depth Information
Finally, the last three lines of code in the main method are:
System.out.print("<");
writeSpaces(spaces1 * spaces2 - 5);
System.out.println(">");
This code prints a less-than character at the beginning of the fourth line of output and
then makes a final call on the writeSpaces method. This time the actual parameter
is an expression, not just a variable or literal value. Thus, before the call is made, the
computer evaluates the expression to determine its value:
spaces1 * spaces2 - 5
3
*
5
- 5
15
- 5
10
The computer uses this result to initialize number :
method main
method writeSpaces
spaces1
3
spaces2 5
number
10
Now number is a copy of the value described by this complex expression.
Therefore, the total output of this program is
* *
! !
' '
< >
Common Programming Error
Confusing Actual and Formal Parameters
Many students get used to seeing declarations of formal parameters and mistakenly
believe that their syntax is identical to that for passing actual parameters. It's a
common mistake to write the type of a variable as it's being passed to a parameter:
writeSpaces(int spaces1); // this doesn't work
Continued on next page
 
Search WWH ::




Custom Search