File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Buffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String Buffer" Watch "String Buffer" New topic
Author

String Buffer

Sunita Vontel
Ranch Hand

Joined: Aug 28, 2000
Posts: 72
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

Joined: Jun 14, 2000
Posts: 347
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

Joined: Aug 18, 2000
Posts: 39
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

Joined: Aug 18, 2000
Posts: 39
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

Joined: Aug 28, 2000
Posts: 72
Thank u so much stephanie and sandeep.
It realy helped me a lot.
 
 
subject: String Buffer