Section 4.3.1 of the Java Language Specification states that, "There may be many references to the same object.".
But it also states that "The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.
So, my understanding is that "There may be many pointers to the same object.", but how can we have many pointers, which mean distinct addresses, that all refer to the same object? Can a certain object have different addresses?
It just means 'many variables can all be referencing the same thing'.
List<String> list1 = new ArrayList<String>();
List<String> list2 = list1;
list1.add("Hello");
System.out.println(list2);
The above code prints 'Hello' even though we never invoked list2.add
. That's because both the list1
variable and the list2
variable are referencing the same object, therefore, if you add something to the object you reach when dereferencing list1
, it's also visible if you dereference list2
(which the println code ends up doing).
Variables in java (be it fields, locals, or parameters) of non-primitive types are like maps to treasure buried in the sand. The .
operator (as well as []
, synchronized (x)
and a few others) 'dereference' - as in, they mean, in java-terms: Follow the map and dig.
Section 4.3.1 is trying to say: For any given treasure, there can be many many maps out there that all lead to it.
Don't think about references as having a numeric value that indicates where it lives in memory. That's a C thing (yes, java sort of works like that under the hood, but that's the point: It is under the hood; it's impossible to ever see this number, interact with it, do arithmetic on it, etc. In fact, on most JVMs, that 'number' isn't a memory address, not directly anyway).
You seem to interpret that as: For any particular object, many different 'memory addresses' can refer to it. That's not what it means. It just means: Multiple variables can hold the same value.
It's as trivial as:
int x = 5;
int y = 5;
Perhaps you're a tad confused because this seems incredibly obvious, but that's nevertheless what §4.3.1 is spelling out.