Skip to content

Instantly share code, notes, and snippets.

@hakaneroztekin
Last active August 23, 2019 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakaneroztekin/5fba02b69c58c4c7699ff65a88fcd3dc to your computer and use it in GitHub Desktop.
Save hakaneroztekin/5fba02b69c58c4c7699ff65a88fcd3dc to your computer and use it in GitHub Desktop.
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