• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please help!

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
public class Test{
public static void main(String[] args){
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("hello");
Test t = new Test();
t.Buftest(sb1,sb2);
System.out.println(sb1);//1
System.out.println(sb2);//2
}
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
hello
Could anyone explain this to me....why aren't sb1 and sb2 same?
Whats happening?
Thanks,
Rashmi
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
to
s += " there";
a new String is created, so you would have
sb1 ---> "hello"
sb2 ---> "hello" <--- s2<br /> s1 ---> "hello there"
Hope it helps
Detlev
 
Rashmi Hosalli
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much,Detlev.It really helped.
Regards,
Rashmi
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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).]
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic