wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A String Buffer question,help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A String Buffer question,help!" Watch "A String Buffer question,help!" New topic
Author

A String Buffer question,help!

Luyan Sun
Greenhorn

Joined: Feb 08, 2002
Posts: 4
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);
}
}
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
luyan,
we'd like you to read the Javaranch Naming Policy and register again.
Thank you.
Luyan Sun
Greenhorn

Joined: Feb 08, 2002
Posts: 4
oh,I really appreciate your message!
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
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);
}
}


<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: A String Buffer question,help!
 
Similar Threads
Question from Javacaps mock
String buffer
about reference
I thought it should be "Hello" instead of "Hello how are you"
don't understand