Java Reference
In-Depth Information
The preceding program works as follows: The statement in Line 5 declares str to be a
reference variable of the StringBuffer type and assigns the string "Hello" to it (see
Figure 7-13).
main
str
Hello
FIGURE 7-13 Variable after the statement in Line 5 executes
The statement in Line 6 outputs the first line of output. The statement in Line 7 calls the
method stringBufferParameter . The actual parameter is str and the formal para-
meter is pStr . The value of str is copied into pStr . Because both of these parameters
are reference variables, str and pStr point to the same string, which is "Hello" (see
Figure 7-14).
stringBufferParameter
main
pStr
str
Hello
FIGURE 7-14 Variable before the statement in Line 7 executes
Then control is transferred to the method stringBufferParameter . The next state-
ment executed is in Line 12, which produces the second line of the output. The
statement in Line 13 produces the third line of the output. This statement also outputs
the string to which pStr points, and the printed value is that string. The statement in
Line 14 uses the method append to append the string " There" to the string pointed to
by pStr . After this statement executes, pStr points to the string "Hello There" .
However, this also changes the string that was assigned to the variable str . When the
statement in Line 14 executes, str points to the same string as pStr (see Figure 7-15).
stringBufferParameter
main
pStr
str
Hello There
FIGURE 7-15 Variable after the statement in Line 14 executes
 
Search WWH ::




Custom Search