AssignmentCopiesObject
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 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.
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.
Language
Concepts
Expressible In
Related Misconceptions
Other Languages
Literature References
The following papers directly or indirectly provide qualitative or quantitative evidence related to this misconception.
Quantitative systematic research
97 Swiss high school teachers and their students
Programming Language Misconceptions studied:
Quantitative systematic research
Students from 2 universities and other users of online textbook
Programming Language Behavior Misconceptions studied: