Skip to content

Instantly share code, notes, and snippets.

@d3ep4k
Created July 3, 2015 12:57
Show Gist options
  • Save d3ep4k/4176203c357c5c73a11f to your computer and use it in GitHub Desktop.
Save d3ep4k/4176203c357c5c73a11f to your computer and use it in GitHub Desktop.
Java Pass by Value or Pass by Reference
class Dog{
String name
Dog(String name){
this.name = name;
}
String getName(){ return this.name;}
}
Dog aDog = new Dog("Max");
foo(aDog);
if( aDog.getName().equals("Max") ){ //true
System.out.println( "Java passes by value." );
}else if( aDog.getName().equals("Fifi") ){
System.out.println( "Java passes by reference." );
}
def foo(Dog d) {
d.getName().equals("Max"); // true
d = new Dog("Fifi");
d.getName().equals("Fifi"); // true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment