A statement like
String dest = origin creates a copy of object
origin and assigns that copy to the variable
Assignment copies the object
Assignment copies the reference pointing to the object
CorrectionHere is what's right.
An assignment of a reference to a variable copies the reference, not the object the reference points to.
In Java, a variable can only ever store a reference to an object. Variables in Java cannot store an object itself. So if you copy the value of one variable into another variable, you copy the reference, not the object.
So, for example:
Pacman p1 = new Pacman(); Pacman p2 = p1;
The first line of the above code creates a
and assigns a reference to that object to variable
The second line copies the reference that is stored in
p1 into variable
At the end of this code, there still is only a single
And there are two variables,
p2 that both point to that same object.
ValueHow can you build on this misconception?
This can provide an opportunity to discuss value types (inline classes), which require one to give up on identity.