passing by reference in Java doubts
So I was reading this post and response no. 2. In that example, after calling that method, does the Dog value at address 42, name's changes to Max?
Dog myDog;
Dog myDog = new Dog("Rover");
foo(myDog);
public void foo(Dog someDog) {
someDog.se开发者_运维百科tName("Max"); // AAA
someDog = new Dog("Fifi"); // BBB
someDog.setName("Rowlf"); // CCC
}
Java is pass by value - always, both for primitives and objects.
In the case of objects, the thing that's passed is the reference to the object that lives out on the heap. A method cannot change what that reference points to when it's passed in.
If that reference points to an object that has mutable data, the method can alter its state.
From "The Java Programming Language Second Edition", by Ken Arnold and James Gosling (ISBN 0-201-31006-6 ) (probably from page 40--don't have the book handy right now):
Some people will say incorrectly that objects in Java are "pass by reference." The term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. [...] There is exactly one ParameterPassing mode in Java--pass by value--and that helps keep things simple.
So let's look at your example (with some improvements):
public class Dog {
private String name;
public static void main(String [] args) {
Dog myDog = new Dog("Rover");
System.out.println("before foo: " + myDog);
foo(myDog);
System.out.println("after foo: " + myDog);
}
public static void foo(Dog someDog) {
someDog.setName("Max"); // AAA
someDog = new Dog("Fifi"); // BBB
someDog.setName("Rowlf"); // CCC
}
public Dog(String n) { this.name = n; }
public String getName() { return this.name; }
public void setName(String n) { this.name = n; }
public String toString() { return this.name; }
}
Here's the output:
before foo: Rover
after foo: Max
Tool completed successfully
You can't change what the reference that's passed to foo
points to, so setting it to the reference with the name "Fifi" at line BBB, and subsequently changing the name of that object at line CCC, does nothing. That instance is eligible for garbage collection when foo
exits.
The incoming reference that points to "Rover" has a mutable data member: its name. Changing its value at line AAA is reflected in the reference that was passed in; hence the different output.
This is probably the topic that is confusing for most novice developers in Java. duffymo had already provided a detailed answer (I'm not going to repeat that) but I could give you some articles for reference that I hope would clear the doubt from your mind.
Jon Skeet's article about parameter passing in Java
Pass-by-Value Please
Does Java pass by reference or pass by value?
HTH.
that Dog (the one at address 42) is asked to change his name to Max
Yes
Does the Dog value at address 42, name's changes to Max?
Yes. The name
changes
Dog myDog = new Dog("Rover");
foo(myDog);
public void foo(Dog someDog) {
someDog.setName("Max"); // AAA
someDog = new Dog("Fifi"); // BBB
someDog.setName("Rowlf"); // CCC
}
myDog
and someDog
share the same object and contain the same address value at the beginning. That is, both variables tell the JVM how to get to the Object Dog in memory (heap). So if you change something by invoking the setName
method on someDog
, myDog
'will feel' the effects!
After someDog = new Dog("Fifi")
a new Object Dog is created (Fifi) and the someDog
variable contains a new value/address for Fifi. That is, the value of someDog
changes and will tell the JVM how to get to Fifi from now on. At this point, every change on someDog
won't affect myDog
anymore.
Check my answer (the one with the picture) on that post and note that I didn't use the word "pointer" and not even the word "reference" this time! ;)
精彩评论