Java Reference
In-Depth Information
The net effect of this process is that the writeSpaces method has a local copy of
the value stored in the variable spaces1 from the main method. The println that
comes after the call on writeSpaces puts an asterisk at the end of the line and then
completes the line of output.
Let's now trace the next three lines of code:
System.out.print("!");
writeSpaces(spaces2);
System.out.println("!");
The first line prints an exclamation mark on the second line of output, then calls
writeSpaces again, this time with the variable spaces2 as its actual parameter. The
computer evaluates this expression, obtaining the result 5 . This value is used to initialize
number . Thus, this time it creates a copy of the value stored in the variable spaces2
from the main method:
method main
method writeSpaces
spaces1
3
spaces2 5
number
5
Because number has a different value this time ( 5 instead of 3 ), the method pro-
duces a different number of spaces. After the method executes, the println finishes
the line of output with a second exclamation mark.
Here are the next three lines of code:
System.out.print("'");
writeSpaces(8);
System.out.println("'");
This code writes a single quotation mark at the beginning of the third line of out-
put and then calls writeSpaces again. This time it uses the integer literal 8 as the
expression, which means that it initializes the parameter number as a copy of the
number 8:
method main
method writeSpaces
spaces1
3
spaces2 5
number
8
Again, the method will behave differently because of the different value of number .
It prints eight spaces on the line and finishes executing. Then the println completes
the line of output by printing another single quotation mark at the end of the line.
 
Search WWH ::




Custom Search