AssignCompares

Incorrect

= compares two values

Correct

= assigns a value to a variable

Correction
Here 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.

Origin
Where could this misconception come from?

This misconception may stem from prior knowledge in mathematics, where = means equals (i.e., is a comparison operator).

Symptoms
How 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) {
  ...
}

Value
How 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

Java

Concepts

Expressible In

Related Misconceptions

Other Languages

Literature References

The following papers directly or indirectly provide qualitative or quantitative evidence related to this misconception.

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.