Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

confused by the StringBuffer

Thread view: 
Ye Dafeng - 17 Oct 2006 08:50 GMT
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
Ye Dafeng - 17 Oct 2006 08:59 GMT
> I have a program as follows:
> public class Test{
[quoted text clipped - 17 lines]
> 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.
Dijon Yu - 18 Oct 2006 13:00 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.