Author Topic:   StringBuffer
Umesh
ranch hand
posted March 27, 2000 11:32 PM             

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
}
}

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.

Frank Tamminga
greenhorn
posted March 28, 2000 12:15 AM         
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).]

maha anna
bartender
posted March 28, 2000 10:20 AM             
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

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;
}
}

Umesh
ranch hand
posted March 29, 2000 09:13 PM             
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
bartender
posted March 29, 2000 09:29 PM             
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).]

|