I have the following code. I can't, however, understand it's behavior about pass by value and reference.
class Dog{
String name;
int x=100;
Dog(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
class Demo{
public static void main( String[] args ){
Dog aDog = new Dog("Tom");
foo(aDog);
if (aDog.getName().equals("Tom")) { //true
System.out.println( "Java passes by value."+ aDog.getName());
System.out.println( aDog.x);
} else if (aDog.getName().equals("Taz")) {
System.out.println( "Java passes by reference." );
}
}
public static void foo(Dog d) {
++d.x;
d = new Dog("Taz");
++d.x;
}
}
This will provide output as
Java passes by value.Tom
101
Why is the output 101
? I was expecting output 102
.
You're incrementing x
twice, but on different dogs. Look at this code:
public static void foo(Dog d) {
++d.x;
d = new Dog("Taz");
++d.x;
}
Initially, d
refers to the dog with a name of Tom
, with x=100
. That isn't a copy of the original object - it's a reference to the same object. Java passes the reference by value, which isn't the same as either passing an object by value or passing an object by reference. It's important to understand that the value of aDog
in main
isn't a Dog
object - it's a reference to a Dog
object. The value of that reference is passed, by value, to your foo
method... so the initial value of d
is the same as the value of aDog
. Further changes to the d
variable itself (rather than the object its value refers to) do not change the aDog
variable.
So, looking at the rest of foo
:
++d.x
, d
refers to the dog with a name of Tom
, with x=101
.d = new Dog("Taz")
, d
refers to a dog with a name of Taz
, with x=100
.++d.x
, d
refers to the dog with a name of Taz
, with x=101
.The calling code only ever knows about the dog with a name of Tom
, so it prints out Tom 101.
See more on this question at Stackoverflow