AssignmentCopiesObject

Misconception:

A statement like String dest = origin creates a copy of object origin and assigns that copy to the variable dest.

Incorrect

Assignment copies the object

Correct

Assignment copies the reference pointing to the object

Correction
Here 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 Pacman object and assigns a reference to that object to variable p1. The second line copies the reference that is stored in p1 into variable p2. At the end of this code, there still is only a single Pacman object. And there are two variables, p1 and p2 that both point to that same object.

Value
How 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.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.