• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

A String Buffer question,help!

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,I meet a interesting question about this string buffer,why this output is "Hello how are you",but not "doing"? I'm confused about it.
public class TestBuffer {
public void myBuf( StringBuffer s2, StringBuffer s3) {
s2.append(" how are you") ;
s2 = s3;
}
public static void main ( String args[] ) {
TestBuffer tb = new TestBuffer();
StringBuffer s = new StringBuffer("Hello");
StringBuffer s1 = new StringBuffer("doing");
tb.myBuf(s, s1);
System.out.print(s);
}
}
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a couple of different things going on here:
You have two StringBuffer objects, because you call new StringBuffer twice, lets call them object1 and object2. The contents of object1 are "Hello" and object 2 are "doing".
Initially the reference s points to object1 and s1 points to object2.
Now when you make the call to myBuf you get copies of these references passed, so now s2 also points to object1 and s3 also points to object2

The append method adds " how are you" to object1, it now contains "Hello how are you".
Now here is the problem line s2 = s3, says make s2 point to object2. Fine, but now the method terminates, s2 and s3 go out of scope, that is they cease to exist. Note it is only the object references, not the objects themselves which disappear.
Now when you return to the main method and println(s) what is you get is the contents "Hello how are you".
To get doing at the end you need another append statement.
Hope this helps
Graeme
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by luyan:
hi,I meet a interesting question about this string buffer,why this output is "Hello how are you",but not "doing"? I'm confused about it.
public class TestBuffer {
public void myBuf( StringBuffer s2, StringBuffer s3) {
s2.append(" how are you") ;
s2 = s3;
}
public static void main ( String args[] ) {
TestBuffer tb = new TestBuffer();
StringBuffer s = new StringBuffer("Hello");
StringBuffer s1 = new StringBuffer("doing");
tb.myBuf(s, s1);
System.out.print(s);
}
}


Assigning one StringBuffer object to another does not append the contents of that StringBuffer to the other one. To relate that to your example:

Hopefully, this makes sense. In short, if you want to append a String to a StringBuffer, you need to use the append method. Simply assigning one to the other changes the reference of the variable you've assigned to.
HTH,
Corey
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
luyan,
we'd like you to read the Javaranch Naming Policy and register again.
Thank you.
 
Luyan Sun
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh,I really appreciate your message!
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to remember you are passing a "copy" of the reference to string buffer objects s and s1 not the actualy references, so within the method of myBuf(), the assignment s2 = s3 is working with the "copy" of the references, so the original references don't get altered.

Originally posted by luyan:
hi,I meet a interesting question about this string buffer,why this output is "Hello how are you",but not "doing"? I'm confused about it.
public class TestBuffer {
public void myBuf( StringBuffer s2, StringBuffer s3) {
s2.append(" how are you") ;
s2 = s3;
}
public static void main ( String args[] ) {
TestBuffer tb = new TestBuffer();
StringBuffer s = new StringBuffer("Hello");
StringBuffer s1 = new StringBuffer("doing");
tb.myBuf(s, s1);
System.out.print(s);
}
}

 
reply
    Bookmark Topic Watch Topic
  • New Topic