VariablesHoldObjects
A statement like String s = "Hello";
stores the String
object in the variable s
such that the entire string is stored inside the variable.
A variable of a reference type contains a whole object
A variable of a reference type contains a reference to an object
CorrectionHere 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.
SymptomsHow 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.
ValueHow 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.