• 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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, i am new to this forum , i am having difficulty in understanding this code can anyone pls explain to me ?
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;
}
}
What will be the output?
Answer:
a. a is One
b is Two
b. a is One
b is One
c. a is One more
b is One more
d. a is One more
b is Two
e. a is One more
b is Two more

The Ans mentioned is D which is correct, i compiled and chked but i did not understand this can anyone pls explain to me.

------------------
Find A Purpose In Life So Big That It Will Challenge Every Capacity To Be At Your Best.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ! Bhavana
In Java the key is to remember that we always pass a copy of the argument , so when you called Test.swap(a,b), copies of a and b were passed. However , in the swap() method, you are operating on the string buffer pointed to by a, that is changing the contents of the string buffer, the contents of a are now One More. In the last line of swap() you are assiging b=a (but note the b here is a copy of the original b, which point to the string buffer that has "Two" in it.
Hence the result is "D".
I hope this helps.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Latha, per your explanation a should have been the answer!
Bhavana, here is what is going on. When an object (as opposed to primitive) is passed as
argument a copy of its reference (which is a 32 bit address) is passed. The called method
cannot affect the original value of the argument. However if the called method modifies the
object via the reference that was passed the caller will notice the change.
by calling the append(), method swap() is modifying the object via its reference. This changes the original value. StringBuffer being mutable helps it. A little gotcha when
objects are arguments!
b = a; makes b = "One more" inside the method but does not affect the original value of object b. Because the method cannot change the original value of its arguments.
that is why output is
d. a is One more
b is Two
if you modified the swap method as follows
static void swap (StringBuffer a, StringBuffer b) {
a=new StringBuffer("more");
b=a;
}
}
then the output will be
a. a is One
b is Two

[This message has been edited by sanjay kanungo (edited December 14, 2000).]
 
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic