aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pass by Value again Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pass by Value again" Watch "Pass by Value again" New topic
Author

Pass by Value again

luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Given the following code:

I don't understand why "b" in main method didn't change, even though it had been changed in method "swap". Note that "a" has been changed successfully in both methods. It is so strange to me.
Luk
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
<pre>

public class TestpassStringBuffer {
public static void main(String args[]) {
StringBuffer a = new StringBuffer("One");
StringBuffer b = new StringBuffer("Two");
TestpassStringBuffer.swap(a,b);
System.out.println("a is "+ a +"\nb is " + b);
}
static void swap (StringBuffer ina, StringBuffer inb) {
ina.append(" more");
inb=ina;
System.out.println(ina);
System.out.println(inb);
}
}
</pre>
I took the liberty of changing the variables names in the swap method for clarity. Notice that they do not have to be the same as the variables that are used to CALL the method because they are local to this method.
ina is a copy of the a variable holding the same reference as a.
Same for inb.
When you said ina.append(" more"); you were going through the reference and acting directly on the object.
When you said inb=ina; you were only acting on the inb variable and setting it to the same reference as ina. Of course inb is about to DIE so it really did nothing. The b variable was not changed.

[This message has been edited by Cindy Glass (edited March 06, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

When you write code like
<pre>
Object a = new Object();
</pre>
you are actually doing two things: 1) creating an instance of Object (new Object()) somewhere in memory and 2) creating a reference to that instance (Object a =).
If subsequently, you write
<pre>
Object b = a;
</pre>
what you do is simply create another reference to an Object and set it to reference the same Object that 'a' references. You will still have one object, but now you have two references to it ('a' and 'b').
In short, there is a difference between references to objects and the objects themselves. When calling methods of an object, you can treat a reference as if it were the object itself. But when assigning references or passing references as parameters, you have to remember the difference.
- All parameters in Java are passed by value. This means that whatever you do to the variable that receives the value (the formal parameter), it will not affect whatever was actually passed in (the actual parameter).
For example:
<pre>
Object a = new Object;
someMethod(a); // 'a' is the actual parameter
...

void someMethod(Object x) { // 'x' is the formal parameter
// 'x' references the same Object that the actual parameter references
System.out.println(x);
x = new Object();
// now 'x' references the new Object
// this does not affect 'a' though
}
</pre>

part of your confusion may stem from the fact that you are using the same variable names in main() and swap(). It may help you understand better if you change the variable names in swap() from 'a' and 'b' to 'x' and 'y', respectively.
In your code, you only have two StringBuffer objects, the ones created in main() with new. The rest of the code just deals with references to those two objects.
J.Lacar


Junilu - [How to Ask Questions] [How to Answer Questions]
luk Hann
Ranch Hand

Joined: Jan 29, 2001
Posts: 48
Hey Cindy and Junilu,
Thanks you guys so much! You guys really really gave me a clear explainations here. This actually related to some fundermental concepts about object and object reference. In my self-studying, I actually have spent a lot of time on these. I also read JavaRanch's Campfire article.
I caught almost all your sayings except one sentence from Cindy's posting:

When you said ina.append(" more"); you were going through the reference and acting directly on the object.

And i think this is the core here. So could you guys gave me a little bit further explaination on how ina.append(" more") acted directly on the object?
BTW, if i changed StringBuffer object to String, then both a and b are not effected by method swap(). Is this simply becuase String is immutable? Waiting for your further comments on this. Thanks.
luk
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Pass by Value again