confused by the StringBuffer

Y

Ye Dafeng

I have a program as follows:
public class Test{
public static void main(String[] main)
{
StringBuffer test3 = new StringBuffer("A");
StringBuffer test4 = new StringBuffer("B");
operate(test3,test4);
System.out.println(test3 + "," + test4);
}

public static void operate(StringBuffer a, StringBuffer b)
{
a.append(b);
b = a;

System.out.println("b:" + b);
}
}

why the output is "AB,B" not "AB,AB"?
thanks
 
Y

Ye Dafeng

Ye said:
I have a program as follows:
public class Test{
public static void main(String[] main)
{
StringBuffer test3 = new StringBuffer("A");
StringBuffer test4 = new StringBuffer("B");
operate(test3,test4);
System.out.println(test3 + "," + test4);
}

public static void operate(StringBuffer a, StringBuffer b)
{
a.append(b);
b = a;

System.out.println("b:" + b);
}
}

why the output is "AB,B" not "AB,AB"?
thanks

oh, i just got it.
Actually, the b is a copy of the reference. So b = a, will change the
copy, not the actual reference.
 
D

Dijon Yu

Ye said:
oh, i just got it.
Actually, the b is a copy of the reference. So b = a, will change the
copy, not the actual reference.


Yes, in memory, b is just a 32bit address, so b=a , it is not change
the object which is point by original reference .

Dijon Yu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top