• 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

StringBuffer

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}

The o/p for the above code is
a is One more
b is Two

Understand StringBuffer is an object so the reference of the object a and b are passed to the method. In that case if any changes are made to a or b inside the method should'nt it reflect after the method call. Why is it that the changes to a alone is reflected whereas stays as is. ??

Thanks,
JP
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by JP Ravi:
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;
}
}

The o/p for the above code is
a is One more
b is Two

Understand StringBuffer is an object so the reference of the object a and b are passed to the method. In that case if any changes are made to a or b inside the method should'nt it reflect after the method call. Why is it that the changes to a alone is reflected whereas stays as is. ??

Thanks,
JP



imagine a to occupy memory address 100 and b 200.Now 100 has "One" and 200 "Two".When we make method call 100 and 200 are passed.To the contents of 100 we are appending more.While local variable b which has 100 is now pointing to 200 in local method swap.This doesnt affect the varaible b in method main which still is pointing to 200.

Hence the ouput is ne more (100 contents appended)
Two (200 contents not changed)
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, still dont get it. If a.append is changing the value in a , why b=a is not changing the value of b ??

Can anyone give an alternative explanation...

Regards,
JP
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravi,
When you pass objects as parameters to methods, java doesn't pass their references, but it passes another reference which still points to the same object. So if you have the object 'a' pointing to 100, then a is the reference and 100 is the object. Assume that 100 is stored in a memory location 0x1234. a is a reference to that memory location. Now when you send it as a parameter, then java creates one more reference which still points to the same memory location. So effectively you are having one object with two references.
If you are clear till that point, whatever change you make to the object inside the method will get reflected in the calling method. But whatever change you make to the reference just stays there. It doesn't come back to the calling method. if you call a.append(), it operates on the object. But in the second statement, you are redirecting the duplicate reference. So it is not affecting the object which is stored in the place where the reference was pointing to previously. So b has the same value.

Hope i am clear.

Thanks,
Gokul.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very clearly put Gokul. The key is to understand that append modifies the object and not the reference.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u gokul for ur fabulous explanation.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so thats also why java has a call by value, although most of the times your values are references..
 
author
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys, let me try to explain it in a different way... The trick here is that LOCAL variables of swap() are named the same as local variables of the main() and Java also passes copies, even if those are the copies of references...

swap() could be easily written like this:

static void swap (StringBuffer a999, StringBuffer b999)

Exactly the same thing.

a999 is given the reference to a and whatever is done to a999 is done to a.

But then b999, instead reference to b, gets reference to a. All right, and after this nothing happens, the method completes, b999 disappears. b stays the same, whatever it was.
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it now...thanks for all ur responses.
 
Let's go to the waterfront with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic