• 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

StringBuffer

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class StrBufTest {
public void method1(StringBuffer s1, StringBuffer s2){
s1.append("There");
s2 = s1;
}
public static void main(String[] args){
StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");
StrBufTest sbt = new StrBufTest();
sbt.method1(sb1, sb2);
System.out.println("sb1 is " + sb1 + "\nsb2 is " + sb2);
}
}
The above code outputs HelloThere and Hello
I did'nt get why?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the first part is clear (s1 = "HelloThere") .Regarding the second part when u assigned s2=s1 . The local reference in the method1 argument now points to s1 ("HelloThere") however the reference s2 in the main method still points to "Hello". Using reference if you alter the stringbuffer contents then that will be reflected in the main method too , but simply changing the reference in method1 wont harm the main method reference.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The
s2 = s1;
inside the method doesn't do absolutely anything in this example since the methods ends right after.
s1 and s2 are LOCAL variables inside the method that contain COPIES of the references sb1 and sb2. The method can do whatever it wants with those and sb1 and sb2 will never see the difference.
But because s1 and s2 are reference variables, you can use them to change the state of the objects they represent.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone pls exlain it to me that why
s1.append("There");makes sb1=hello there when s1 is also a local reference variable.
Thnx

[This message has been edited by Raj Mehra (edited January 16, 2001).]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
will anyone explain why the value of s1 is reflected in main if value of se is not reflected?
total confusing for me?
------------------
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajneesh,
First thing, there's only pass by value in java.
I'll try to visualize the process
*sb1, sb2 are constructed:
sb1 --> "Hello"
sb2 --> "Hello"
*In method1(), since s1, s2 are copies of sb1, sb2, then
sb1 --> "Hello" <-- s1<br /> sb2 --> "Hello" <-- s2<br /> *s1.append("There")<br /> sb1 --> "HelloThere" <-- s1<br /> sb2 --> "Hello" <-- s2<br /> * s2 = s1;<br /> <br /> <br /> sb2 --> "Hello"
In main, sb1 --> "HelloThere"
sb2 --> "Hello"
Hope this'll help



[This message has been edited by Son Le (edited January 16, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic