Skip to content

Instantly share code, notes, and snippets.

@tosinonikute
Created May 17, 2017 12:18
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 tosinonikute/0ab5d6460c004475a92811aadad85125 to your computer and use it in GitHub Desktop.
Save tosinonikute/0ab5d6460c004475a92811aadad85125 to your computer and use it in GitHub Desktop.
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