public class Test { public static void main(String args[]) {StringBuffer a = new StringBuffer("One");StringBuffer b = new StringBuffer("Two"); Test.swap(a,b); System.out.println("a is "+ a +"\nb is " + b); } static void swap (StringBuffer a, StringBuffer b) { a.append(" more"); b=a; } } answer is a is One more b is Two Why it is not a is One more b is One more

What is the actual process taken ? This is because reference in variable a is assigned to local variable b (as b is a local variable of swap method). This assignemnet is absolutely local to the swap method and it will not reflected in the variable b of main method. Hope this will help Asif

Richard Selva
Greenhorn

Joined: Apr 10, 2002
Posts: 15

posted May 02, 2002 05:58:00

0

This assignemnet is absolutely local to the swap method and it will not reflected in the variable b of main method. if so, appneding the text "more" also to the local reference a in swap method . Then how it is reflecting to the varicable a in the main method ?? Have little confusion on this selva

If you visualize/read the things like this you will be clear to understand the output. a.append("more") => you are appending data to the Object whose reference is stored in variable 'a'. b = a => assigning reference value stored in 'a' to 'b' where both a & b are local variables in given program. So, you append data to object not to the reference. regards Kapil

In case of apending text, you are operating on original reference to the object which is "one" that'y its being reflected in the main method. I hope following description will help to understand things better. In Main Method a => "one" b => "two" Here variable a and b are local to main method which contains reference to two objects "one" and "two" respectively. swap method call: swap(a , b) through this method call, only reference to the two objects "one" and "two" is passed, not variables. In Swap Method public static void swap(StringBuffere a, StringBuffere b) The above line of code, creates its own variables a and b (which are now local to the Swap method and has nothing to do with the ones in main method) and holds the "reference" to two objects "one" and "two" respectively created in the main method. a.append("more"); As variable a contains reference of exactly that object which was created in main method, that's why it will be updated now to "onemore" and as variable a in main method also points to this object that's why this change will be reflected there in main method. b = a; When the above line of code will be executed, it will assign reference in variable a (to "onemore" object) to variable b. Now variable b also points to the object "onemore". But this assignment is local to this method because we decided already that variables a and b in swap method are local to only this method, that's why this reference assignment will not reflected in the main method. Hope this will help.... Asif

when passing refence,actually a copy is passed.u also can treat it there is another refence refer to the same object. in the method,if you oparate the copy-refence directly ,because it refer to the same object as the outer one,so the object is changed.but if you make the copy-refence refer to a new onject such as u use "new" or "=" etc,just the copy-refence has new value,but the outer one is still not changed.

Asif, quote: As variable a contains reference of exactly that object which was created in main method, that's why it will be updated now to "onemore" and as variable a in main method also points to this object that's why this change will be reflected there in main method. can you tell me how different the situation of b is than a? what i am saying is, if 'a' contains reference of 'exactly' that object which was created in main method... will b also not contain its corresponding reference created in main? please explain.

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271

posted May 06, 2002 06:44:00

0

Originally posted by imran anwar: can you tell me how different the situation of b is than a? what i am saying is, if 'a' contains reference of 'exactly' that object which was created in main method... will b also not contain its corresponding reference created in main?

Imran, The key to understanding this is to see that the variable b in main and the paramater b in the method are two distinct variables. They are both reference variables, which reference a StringBuffer object. When swap is called, both b in main and b in swap contain the same value (a copy of b in main is sent to the method swap). Therefore, both reference variables reference the same object. However, in the last line of the swap method, we're changing the variable b in the swap method. Note that we're not making any sort of modification to the object referenced by b, we're just changing what it references. Therefore, after that line is executed, the variable b in swap references a different object than the variable b in main. Take a look at the Flash app that I linked to in the earlier post - hopefully, it'll help clear this up for you. I hope that helps, Corey