• 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 Question

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3) Consider the following code,
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?

Ans given is: a one more
b is two.
I answered a is one more, b is one more. Don't understand why b is assigned two?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the variable b declared as the second parameter to the method swap is another variable distinct from the other b variable declared outside it. The b parameter variable will only live while the duration of the method. First the b parameter variable will point to the same StringBuffer object as the outside b variable does. Later will point to the StringBuffer pointed by the variable a. When the method ends the two parameter variables will be destroyed. And the objects they pointed will have a reference fewer. But the variables outside the method are not affected by its termination. The object pointed by the outside a has been modified because a.append("more") really modified the object pointed simoultaneouly by both a. The object pointed by the outside b was not modified within the method; only the parameter variable b was assigned a new value (object).
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
The issue here is not so much about StringBuffers as how objects and references are passed, and local variables.
In the method swap the parameters a and b receive the value of the variables given in the method call. In this case, it is the value of the object references a and b from the main method. The value of a reference is a reference (call it the memory address if that makes it easier to think about). So the local variables (local to the swap method) refer to the same objects as the variables in main.
The method call to append on the variable a will effect the object that a references. It will also effect the object that the a in the main method references, because they are the same object.
The next line 'b = a;' assigns the value of a to the variable b. Keep in mind that this does not effect the object that b refers to it effect the value of b. So now b refers to the same object that a does. Note that this the local b variable not the one in main. The b in main still refers to the original StringBuffer object which has not changed.
hope that helps
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose and Dave, I guess I spaced out. here's my own notes.
43. When we pass references in Java, what actually gets passed is the value of that reference i.e. the memory address of the object being referenced. When changes are made to the object itself, those changes are reflected on that object even outside of the method making the call. But any changes to the reference itself do not reflect outside of the method call. But does inside the method call .
16. Variables declared in methods are not accessible from any other method. (Compiler error).
Variables declared within blocks, have the scope of the block. (loop for example)
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey,

Originally posted by Mike Kelly:
...But any changes to the reference itself do not reflect outside of the method call. But does inside the method call .


also adding to the above note, I think as soon you make changes to the reference from within the method, the original reference(the one that was passed in) becomes lost to the method, so that you cannot do any more operations on it, because the method does not know where it went :roll:

16. Variables declared in methods are not accessible from any other method. (Compiler error).
Variables declared within blocks, have the scope of the block. (loop for example)


yeah I always get confused coming from c where you can have local static variables heh...
so long,
mike
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic