This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Behaviour of String and StringBuffer class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Behaviour of String and StringBuffer class." Watch "Behaviour of String and StringBuffer class." New topic
Author

Behaviour of String and StringBuffer class.

Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115


output of the program is,
WO
WO
s1: W
s2: WO

In line 1, when we are passing (String , StringBuffer) as (s1, s2), we are only passing a copy of s1 & s2 to the method in line 2. so any changes done to s1 & s2 inside the method, puto(String s1, StringBuffer s2) should not really change the values of s1 & s2. so in line 3, s1 is printed as "W" which is expected.
But in line 4, s2 is printed as "WO". how come the changes done to s2 in the method, puto(String s1, StringBuffer s2) is reflected, when s2 is printed in main() method.? i expected s2 should be printed "W" too!
can anyone explain why?

SCJP 6.0 90%
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Because Strings are immutable and StringBuffers are mutable. You passed a copy of the reference to the String, and to the StringBuffer. When you "modified" the String, it actually created a new String object and pointed to that. However, since StringBuffers are mutable, when you made a change it was made to the same object that is in the main method, since both variables hold a reference to teh same object.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Nidhi Sar
Ranch Hand

Joined: Oct 19, 2009
Posts: 252

Anuradha,

Actually the output of your program is following:


WO
WO
s1: (space)(space)W(space)(space)
s2: WO



Notice the extra untrimmed space in front of W in case of s1.
And that is exactly what it should be. There are two concepts at play here:

1. When we pass a reference we do pass a copy, but it is copy of the bits, that refer to the SAME object. So there are two references (the original and the copied) but they are both refering to the same object. There is just one object.

After the method is over, any changes that you make to the object itself will be continue to be reflected after the method is over. Any change that you make to the reference (e.g. making it point to a different object) will not be reflected.

2. String object is immutable. When you pass it to a method, you are still passing a copy of reference to the same object (just like in 1). But when you make any changes inside the method, the original String cannot be changed and so a new one is created. Original object is untouched. And ofcourse, that original object is exactly what you have a reference to in the main method.

Please correct me someone if I am wrong!


"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
Thanks for the clarification!

the output was actually,
WO
WO
s1: (space)(space)W(space)(space)
s2: WO

but the spaces before W did not get copied here
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Behaviour of String and StringBuffer class.
 
Similar Threads
StringBuffer parameter passing.
how many String objects are created
Please help!
Que on thread
String Buffer