File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I thought it should be Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I thought it should be "Hello" instead of "Hello how are you"" Watch "I thought it should be "Hello" instead of "Hello how are you"" New topic
Author

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

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 26, 2000
Posts: 1242
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

Joined: May 10, 2001
Posts: 3244
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


Dave
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: May 10, 2001
Posts: 3244
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
 
subject: I thought it should be "Hello" instead of "Hello how are you"