Created
July 3, 2015 12:57
-
-
Save d3ep4k/4176203c357c5c73a11f to your computer and use it in GitHub Desktop.
Java Pass by Value or Pass by Reference
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
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