Created
May 17, 2017 12:18
-
-
Save tosinonikute/0ab5d6460c004475a92811aadad85125 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
public class Refs { | |
int value = 0; | |
// similar to :4 case in the c++ example | |
static void accept_reference(Example e) { // :1 | |
e.value++; // will change the referenced object | |
e = null; // will only change the parameter | |
} | |
// similar to the :2 case in the c++ example | |
static void accept_primitive(int v) { // :2 | |
v++; // will only change the parameter | |
} | |
public static void main(String... args) { | |
int value = 0; | |
Example ref = new Example(); // reference | |
// note what we pass is the reference, not the object. we can't | |
// pass objects. The reference is copied (pass-by-value). | |
accept_reference(ref); // :1 | |
assert ref != null && ref.value == 1; | |
// System.out.println(ref.value); | |
// the primitive int variable is copied | |
accept_primitive(value); // :2 | |
// System.out.println(value); | |
assert value == 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment