ReferenceToVariable

Misconception:

A reference can point to a variable, e.g., to a field in an object or a local variable in a stack frame.

Incorrect

References can point to variables

Correct

References can only point to heap objects

Correction
Here is what's right.

In Java, a reference either is null or it points to an object (or to an array). References cannot point into the insides of an object/array, they cannot point anywhere into the stack, and they cannot point to global (static fields).

A variable holds a value, and in Java a value is either a value of a primitive type, or a reference to some heap object (note that an array also is considered a heap object).

In the example below, the assignment statement t = s does not store a reference to variable s in variable t!

int s = 3;
int t = s;

Here, variable t will store the value that was stored in variable s. That is, variable t will store the value 3.

Origin
Where could this misconception come from?

This misconception might stem from students who have some knowledge of C, where one can take the address of a variable, or of C++ references.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.