my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please help! 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 "Please help!" Watch "Please help!" New topic

Please help!

Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
public class Test{
public static void main(String[] args){
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("hello");
Test t = new Test();
public void Buftest(StringBuffer s1,StringBuffer s2)
s1.append(" there");
s2 = s1;
System.out.println("Inside the method " + s1);//3
System.out.println("Inside the method " +s2);//4
In the above program,the output of lines 3 & 4 are as expected
Inside the method hello there
Inside the method hello there
but the output of lines 1 & 2 are as follows:
hello there
Could anyone explain this to me....why aren't sb1 and sb2 same?
Whats happening?
Detlev Beutner
Ranch Hand

Joined: Jul 13, 2001
Posts: 76
The sentence "A string buffer is like a String, but can be modified." in the StringBuffer-API is a hint...
Ok, I'll try to explain (I took some time myself to realize this concept, but once you got it explained, I think it is easy to understand & to remember):
The line

produces a pointer sb1 to an object, here the String "hello", so the situation is as follows:
sb1 ---> "hello"
The same with the next line, now we will have in the memory:
sb1 ---> "hello"
sb2 ---> "hello"
If you call a method, the parameters, and that is the main point, will be passed as /copies of the pointer/. So after Buftest is called, within the method, at it's beginning, you will have the following situation in the memory:
sb1 ---> "hello" <--- s1<br /> sb2 ---> "hello" <--- s2<br /> With<br /> <br /> you'll get:<br /> sb1 ---> "hello there" <--- s1 = s2<br /> sb2 ---> "hello"
That's all.
If you change from StringBuffer to String and from
s.append(" there");
s += " there";
a new String is created, so you would have
sb1 ---> "hello"
sb2 ---> "hello" <--- s2<br /> s1 ---> "hello there"
Hope it helps
Rashmi Hosalli
Ranch Hand

Joined: Jun 25, 2001
Posts: 50
Thank you so much,Detlev.It really helped.
Anshul Manisha
Ranch Hand

Joined: Apr 17, 2001
Posts: 74

you'll get:
sb1 ---> "hello there" <--- s1 = s2<br /> sb2 ---> "hello"

Java passes by reference so while calling method bufTest references of sb1 and sb2 are passed. Now these references are immutable i.e. you cannot pass back a changed reference to the calling block. If you make any changes to reference inside your called method then the change is done in the copy local to called method. However you can modify the properties of the object referenced. So if you append to sb1(you are modifying the property) it will be reflected in the calling block(prints "hello there" second time) but if you change the reference then the calling block will retain reference to original object(sb2 prints "hello" in main)
Hope that this helps.

[This message has been edited by Anshul Manisha (edited July 15, 2001).]

AM<BR> <A HREF="" rel="nofollow"></A>
It is sorta covered in the JavaRanch Style Guide.
subject: Please help!