Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

doubt with StringBuffer

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i knew immutability of StringBuffers but confused



StringBuffer is mutable as Chandra pointed out, not immutable.

Cheers,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh sorry i meant mutability
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No more posts regarding this particular mistake of putting
immutability in place of mutability
this has been pointed out already
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic