Last active
August 23, 2019 13:04
-
-
Save hakaneroztekin/5fba02b69c58c4c7699ff65a88fcd3dc 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
class TestingPassByValue() { | |
public static void main(String ... args){ | |
// myFavoriteHobby is Playing billard | |
Hobby myFavoriteHobby = new Hobby("Playing billard"); | |
// hobbyBackup is Playing billard | |
Hobby hobbyBackup = myFavoriteHobby; | |
// passing the value. Simply, you can think it | |
// like passing the value of the pointer (e.g. 42) | |
hobbyGame(myFavoriteHobby); | |
// at this point, myFavoriteHobby is Learning challenging stuff. | |
// Below is true. The thing is, they both keep the address 42 | |
// Value @42 is changed but the address, 42, is the same. | |
hobbyBackup.equals(myFavoriteHobby); // true | |
} | |
// hobby is Playing billard (myFavoriteHobby), they both point to the address 42 | |
public void hobbyGame(Hobby hobby){ | |
// hobby is Learning challenging stuff (myFavoriteHobby) | |
hobby.setName("Learning challenging stuff"); | |
// Below, we point address 79, so hobby=79, myFavoriteHobby=42. | |
// Changes won't affect myFavoriteHobby | |
// hobby is Listening some good music | |
hobby = new Hobby("Listening some good music"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment