• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

StringBuffer

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
class Class1{
public static void main(String argv[])
{
StringBuffer sb1 = new StringBuffer("Hi");
StringBuffer sb2 = new StringBuffer("Hi");
Class1 sbt = new Class1();
sbt.method1(sb1, sb2);
System.out.println("sb1 is " + sb1 + "\nsb2 is " + sb2);
}

public void method1(StringBuffer s1, StringBuffer s2)
{
s1.append("Umesh");
s2 = s1; //lineX
}
}
</pre>
Output: sb1 is HiUmesh
sb2 is Hi
My question is why not s1 assigns value to S2 in the code marked lineX and prints following output:
sb1 is HiUmesh
sb2 is HiUmesh
Thanx in advance.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks quite weird doesn't it?
But it is not that difficult.
Method() takes two StringBuffer args. These are copies of the original references to the objects created in the body of main.
In method() you change the object referred to as s1.
Now you assign the temporal reference s1 to s2.
Nice, but it is only a reference. Not the object.
When you leave method(), the original s2 still referres to the original object.
I hope I cleared it out...
[This message has been edited by Frank Tamminga (edited March 28, 2000).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umesh,
I changed your code a little bit in order to provoke thinking. Can u tell what the result and your understanding for the same.
regds
maha anna
<pre>
class Class1{
public static void main(String argv[]){
String sb1 = new String("Hi");
String sb2 = new String("Hi");
Class1 sbt = new Class1();
sbt.method1(sb1, sb2);
System.out.println("sb1 is " + sb1 + "\nsb2 is " + sb2);
}
public void method1(String s1, String s2){
s1.concat("Umesh");
s2 = s1;
}
}
</pre>
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anna,
Here is the output:
sb1 is Hi
sb2 is Hi
My understanding:
String produces immutable objects and any reference to it will modify the original object.
Incase of StringBuffer, its not immutable and original object can be modified thru reference.
Thank you Frank and I need input from Maha Anna for my comments.
Anna, thanks in advance......
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umesh,
In Java String objects are immutable. Meaning once created, it's content NEVER changes. But the StringBuffer object's content is changable.
So for the 1st qstn which you posted used StringBuffer, when calls method1(StringBuffer sb1,StringBuffer sb2), after the method returns , the content of the originally passed StringBuffer is changed to "Hi Umesh".
But when the same method is slightly modified to take String as an arg, after the method is called, eventhough we tried to concat the input arg String sb1 with "Umesh" inside the method(String sb1, String sb2), what happened inside the method was a NEW String object was created, and the original String object passed to this method was UNTOUCHED. This proves the immutablity of String object.
You may find this particular discussion useful.
regds
maha anna

[This message has been edited by maha anna (edited March 29, 2000).]
 
This tiny ad will self destruct in five seconds.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic