This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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).]
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
posted
0
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
posted
0
Thank u so much stephanie and sandeep. It realy helped me a lot.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.