• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

StringBuffer Question

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the following code:

public class Test15{
public static void method(StringBuffer sb){
sb.append(" Added");
sb = 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 please explain why?

Thanks,
Fes
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. you have an object StringBuffer containing "String Buffer"
2. you pass this to a method
3. in this method the sb-object references to the original StringBuffer-object made in main-method and so by calling the append-method, the original object is altered and contains then "String Buffer Added"
4. the line sb = new StringBuffer("Hai") makes a new StringBuffer and sb points to this new object. sb as argument is a copy of the original sb-object. so you have actually 2 variables pointing to a StringBuffer-object (sb_from_main and sb_as_argument). the sb_as_argument points with this line code to another StringBuffer-object (so there are also 2 objects of these: one containing "String Buffer Added" and another containing "Hai").
5. the method method is at the end, so the sb_as_argument was only known in this method and is now out the scope, but the sb_from_main is still pointing to the original StringBuffer-object and that contains still "String Buffer Added" which is printed.

hope i explained it correctly and you got the point
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roel is correct.

Method arguments are copies, and these copies are local to the method scope. So when you pass sb to the method, you have 2 separate sb variables. Both of these point to the same StringBuffer object, so either can be used to modify the object. However, reassigning the local variable to point to a different object has no effect on the other variable.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic