Java Assignments are evaluated Left to Right

I think I’ve just found a mistake in the Java Tutorial.

Maybe I’m just being pedantic, but I think I’ve just found a mistake in the Java Tutorial. It claims:

All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.

The Java™ Tutorials » Language Basics » Operators

What is this supposed to mean? Evaluation from right to left would mean that when you have a line like the following you get the rightmost expression evaluated first:

int x = 42;
x += getNumber();
// The above would be equivalent to:
x = getNumber() + x;

But that’s not how Java actually evaluates this expression. And the JLS 12 (2019-02-08) clearly states in 15.26.2 that “the value of the left-hand operand is saved and then the right-hand operand is evaluated.”

Continue reading “Java Assignments are evaluated Left to Right”