• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

String Buffer

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u so much stephanie and sandeep.
It realy helped me a lot.
 
reply
    Bookmark Topic Watch Topic
  • New Topic