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.
Variables do not store objects themselves: they store references to objects. Thus, what gets copied is the reference, not the object the reference points to.
let p1 = new Pacman();
let 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.
Language
Concepts
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: