Java Reference
In-Depth Information
The preceding program works as follows: The statement in Line 5 declares
str
to be a
String
variable and assigns the string
"Hello"
to it (see Figure 7-9).
main
str
Hello
FIGURE 7-9
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
stringParameter
. The actual parameter is
str
and the formal parameter
is
pStr
, so the value of
str
is copied into
pStr
. Because both these parameters
are reference variables,
str
and
pStr
point to the same string, which is
"Hello"
(see Figure 7-10).
7
main
stringParameter
str
pStr
Hello
FIGURE 7-10
Variable before the statement in Line 7 executes
The control is transferred to the method
stringParameter
. The next statement
executed is in Line 12, which outputs the second line of the output. The statement
in Line 13 outputs the third line of the output. Notice that this statement also
outputs the string referenced by
pStr
and the printed value is the string
"Hello"
.
The next statement executed is in Line 14. This statement uses the assignment
operator and assigns the string
"Sunny Day"
to
pStr
. After the execution of the
statement in Line 14,
str
no longer refers to the same string as does
pStr
(see
Figure 7-11).
Search WWH ::
Custom Search