VariablesHoldObjects

Misconception:

A statement like String s = "Hello"; stores the String object in the variable s such that the entire string is stored inside the variable.

Incorrect

A variable of a reference type contains a whole object

Correct

A variable of a reference type contains a reference to an object

Correction
Here is what's right.

In Java, classes are reference types. Thus, a variable of a class type does not store an object, but it stores a reference to that object.

Point p;         // declare variable (no object allocated)
new Point();     // allocate object (and throw it away)
p = new Point(); // allocate object, store reference to it in variable

Technically, a reference to an object may be the address of that object, or it may be some other unique id.

Symptoms
How do you know your students might have this misconception?

A common symptom of this misconception is the special case where students draw diagrams of memory and some variable boxes contain string literals.

Value
How can you build on this misconception?

This is a great opportunity to start discussing the idea of aliasing: two variables that refer to the same object. One could not create aliases if objects had to live inside variables.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.