Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Buffer

 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the following code

The output is " a is One more " and b is two.
I thought it should be One more for both a and b coz we are passing references right??
why is it this?
[I added UBB CODE tags to your source code to make it more readable. Please try to use them in the future - Ajith]
[This message has been edited by Ajith Kallambella (edited August 31, 2000).]
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunita,
I think if you look at this:
http://www.javaranch.com/ubb/Forum1/HTML/000360.html
it will clear things up for you.
Stephanie
 
Sandeep Potnis
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunita,
Passing a reference, means passing the memory address of an object (StringBuffer in this case)as a value.
For example, lets say that in main(),
StringBuffer a refers to memory address 1234a1 and
StringBuffer b refers to memory address 4321b1
Now when you call swap(a,b), copies of these addresses are passed to the method (remember references as passed as values)
In swap, when a.append(" more"); gets executed , the change is made to the object that a points to and not to the address (1234a1) that a holds.
When b=a; gets executed in swap, the copy of b that was passed to swap now has 1234a1 and it also points to the same object as a. Since this change was made to the copy of b and not to the original value of b in main(), b in main() still has 4321b1.
So when System.out.println("a is "+ a +"\nb is " + b); gets executed in main(), the changed value of a ("One more")and the original value of b("Two") are printed.
Try putting System.out.println("Swap :" + "a is "+ a +"\nb is " + b); in swap after b=a;
I hope that my rambling makes some sense.

Sandeep
 
Sandeep Potnis
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sunita,
Passing a reference, means passing the memory address of an object (StringBuffer in this case)as a value.
For example, lets say that in main(),
StringBuffer a refers to memory address 1234a1 and
StringBuffer b refers to memory address 4321b1
Now when you call swap(a,b), copies of these addresses are passed to the method (remember references as passed as values)
In swap, when a.append(" more"); gets executed , the change is made to the object that a points to and not to the address (1234a1) that a holds.
When b=a; gets executed in swap, the copy of b that was passed to swap now has 1234a1 and it also points to the same object as a. Since this change was made to the copy of b and not to the original value of b in main(), b in main() still has 4321b1.
So when System.out.println("a is "+ a +"\nb is " + b); gets executed in main(), the changed value of a ("One more")and the original value of b("Two") are printed.
Try putting System.out.println("Swap :" + "a is "+ a +"\nb is " + b); in swap after b=a;
I hope that my rambling makes some sense.

Sandeep
 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u so much stephanie and sandeep.
It realy helped me a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic