File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt with StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt with StringBuffer" Watch "doubt with StringBuffer" New topic
Author

doubt with StringBuffer

shyam kumarK
Ranch Hand

Joined: May 06, 2007
Posts: 30
public class Test6{
public static void method(StringBuffer x){
x.append(" Added");
x = new StringBuffer("Hai");
}
public static void main(String a[]){
StringBuffer sb = new StringBuffer("String Buffer");
method(sb);
System.out.println(sb);
}
}

The output is:
"String Buffer Added"

Can someone explain why?


SCJP 1.4
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by shyam kumarK:
public class Test6{
public static void method(StringBuffer x){
x.append(" Added");
x = new StringBuffer("Hai");
}
public static void main(String a[]){
StringBuffer sb = new StringBuffer("String Buffer");
method(sb);
System.out.println(sb);
}
}

The output is:
"String Buffer Added"

Can someone explain why?



StringBuffer and StringBuilder are mutable (can be modified internally). You pass a copy of the reference variable to the method,
so the method will have a new copy of the reference variable that points to the same object the calling method reference variable is pointing to. So whatever modification the called method will do on the StringBuffer the effect will be on the calling method StringBuffer too; In other words there is only one object on the heap and two reference variables pointing to same object.

And yeah, if the called method tries to change the reference, the effect will be local to the method only. There wont be any effect on the calling method StringBuffer.

Thanks,
[ May 06, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
shyam kumarK
Ranch Hand

Joined: May 06, 2007
Posts: 30
i knew immutability of StringBuffers but confused about what happens after reference changes as in:
x = new StringBuffer("Hai");
now i got it Before the reference is changed whatever changes are made will reflect in the original object

thanks bhat
Omer Haderi
Ranch Hand

Joined: Sep 27, 2006
Posts: 42

i knew immutability of StringBuffers but confused


StringBuffer is mutable as Chandra pointed out, not immutable.

Cheers,
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

i knew immutability of StringBuffers but confused about what happens after reference changes as in:



StringBuffer/StringBuiilder are mutable (you can mutate them)



Thanks,
shyam kumarK
Ranch Hand

Joined: May 06, 2007
Posts: 30
oh sorry i meant mutability
shyam kumarK
Ranch Hand

Joined: May 06, 2007
Posts: 30
No more posts regarding this particular mistake of putting
immutability in place of mutability
this has been pointed out already
 
wood burning stoves
 
subject: doubt with StringBuffer