AssignCompares
The =
operator compares two values; e.g., 1=2
will evaluate to false
, because 1 is not equal to 2.
= compares two values
= assigns a value to a variable
CorrectionHere is what's right.
In Java, and in many other languages,
the =
operator does not compare values,
but it assigns the value on its right to the variable on its left.
Thus, a=1
will store the value 1 in the variable a
.
This also means that 1=2
will not compile,
because the left-hand-side of the assignment (1
)
does not represent a variable,
which means that it’s not possible to store a value in it.
The compiler will report an error when it encounters expressions like 1=2
.
OriginWhere could this misconception come from?
This misconception may stem from prior knowledge in mathematics,
where =
means equals (i.e., is a comparison operator).
SymptomsHow do you know your students might have this misconception?
Students may use (inadvertently or not) a single =
operator
in expressions used as conditions.
The most common place where the mistake happens is probably within if
statements,
such as
if (x = 1) {
...
}
ValueHow can you build on this misconception?
In other programming languages (such as Pascal),
the =
operator indeed performs a comparison between its operands.
In those languages, a different operator (e.g, :=
) is used for an assignment.
Language
Concepts
Expressible In
Related Misconceptions
Other Languages
Literature References
Repository mining study (BlueJ Blackbox)
250000+ students across the world
Qualitative (survey of instructors and students)
Unknown number of undergraduate students, professors, SIGCSE members