Created
June 23, 2013 10:05
-
-
Save tenadavila/5844482 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Dog { | |
public static void main(String[] args) { | |
Dog aDog = new Dog("Max"); | |
foo(aDog); | |
aDog.name.equals("Max"); // true | |
} | |
public static void foo(Dog d) { | |
d.name.equals("Max"); // true | |
d = new Dog("Fifi"); | |
d.name.equals("Fifi"); // true | |
} | |
public String name; | |
public Dog(String name){ | |
this.name = name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Paste it here: http://cscircles.cemc.uwaterloo.ca/java_visualize/#mode=display
Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.
In this example aDog.name will still be "Max". "d" is not overwritten in the function as the object reference is passed by value.
(http://stackoverflow.com/questions/40480/is-java-pass-by-reference)