Java Reference
In-Depth Information
main
stringParameter
str
pStr
Hello
Sunny Day
FIGURE 7-11 Variable after the statement in Line 14 executes
The statement in Line 15 outputs the fourth line. Note that the printed value is the string
"Sunny Day" . After the execution of this statement, control goes back to the method
main at Line 8 (see Figure 7-12).
main
str
Hello
FIGURE 7-12 Variable after the statement in Line 15 executes
Therefore, the next statement executed is in Line 8, which outputs the last line of the
output. Notice that str still refers to the same string, which is "Hello" .
The preceding example shows that you should be careful when passing String variables
as parameters.
The class StringBuffer
If you want to pass strings as parameters to a method and want to change the actual
parameters, you can use the class StringBuffer .The class StringBuffer is similar
to the class String . However, strings assigned to StringBuffer variables can be altered.
The class StringBuffer contains the method append ,whichallowsyoutoappend
a string to an existing string, and the method delete , which allows you to delete all the
characters of the string. It also contains other methods to manipulate a string.
 
 
Search WWH ::




Custom Search