Skip to content

Instantly share code, notes, and snippets.

@tenadavila
Created June 23, 2013 10:05
Show Gist options
  • Save tenadavila/5844482 to your computer and use it in GitHub Desktop.
Save tenadavila/5844482 to your computer and use it in GitHub Desktop.
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;
}
}
@tenadavila
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment