Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer

 
Richard Selva
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Asif Masood
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kapil Sachdeva
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this little Flash application to see an example of somthing similar to this. Hopefully, it'll help clear up any remaining questions you might have.
Corey
 
Asif Masood
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Richard Selva
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Asif for your detailed explanation .
 
Steven Sun
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
imran anwar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic