File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I thought it should be "Hello" instead of "Hello how are you"

 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
public class TestBuffer {
public void myBuf(StringBuffer s, StringBuffer s1) {
s.append(" how are you");
s = s1;
System.out.println(s);
}
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);
}
}
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dai niao:
public class TestBuffer {
public void myBuf(StringBuffer s, StringBuffer s1) {
s.append(" how are you");
s = s1;
System.out.println(s);
}
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);
}
}

if it is a String yes but it is StringBuffer.
String is immutable and StringBuffer is not.
The append method actually added the " how are you" to
the original StringBuffer.
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dai
I'm not sure what part you were confused on, Fei already explained how the StringBuffer works vs a String. So, I'll expalin why the object you created in main gets changed in the method you call in case that is what you were confused on...
Here is your original code, I put in line numbers to refer to certain things:

In lines 1 and 2 you are creating 2 new StringBuffers (s and s1) with the values "Hello" and "doing".
In line 3 you are calling the myBuf method and passing it the two StringBuffers you created in lines 1 and 2. Keep in mind that when you pass an object in Java you are passing a reference to the object...
So, in line 4 you are appending " how are you" to the StringBuffer refered to by s. Also, keep in mind that this variable s is local to the method, it is not the same variable as in the main method even though they both refer to the same object.What you did here was change the StringBuffer that s refers to, now that StringBuffer holds "Hello how are you".
In line 5 you assign the variable s to now refer to the StringBuffer refered to by the variable s1, now both s and s1 refer to the same StringBuffer object.
Line 6 just prints out the StringBuffer that s refers to, in this case it is "doing".
When you get back to line 7 in the main mehtod and print out the variable s, you are printing the StringBuffer that s referes to, this is the same StringBuffer that was changed by the myBuf method, so it prints "Hello how are you".
I hope that explains it for you


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
I am also confused. in line 5, s was assigned to s1(print "doing" in line 6, why in line 7 it chose the result of line 4 instead of line 6(this is the last statement of method right?)?
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
as s=s1; and this assigned "doing" to s, why at last in line 7, it chose the result of line 4 instead of line 6 which is the last statement of the method?
 
Dave Vick
Ranch Hand
Posts: 3244
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
xiao
The variable s in myBuf and the variable s in main are two different variables that at one point refer to the same object.
The variable s in the myBuf method just refers to an object in memory. At the beginning of the method it refers to (points to) the StringBuffer "Hello", then the append method is used and the StringBuffer object, not the variable is changed so it contains "Hello how are you". The variable s is still pointing to that StringBuffer.
In line we change it so that the variable s now points to the same StringBuffer that s1 points to, "doing". This is what is printed in line 6.
When we get back to the main method the variable s in main is still pointing to the original StringBuffer that was changed in the myBuf method, so when s is printed in main it prints out "Hello how are you".
hope that helped clear it up, if not let me know and I can draw a picture that might help (although my artistic abilities are somewhat lacking)

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic