Java expression interpretation rules of decrement/increment operators

Viewed 243

This is a purely theoretical question, I wouldn't write this code normally, for clarity's sake.

Why is this quite ambiguous statement legal

int a = 1, b = 2;
int c = a---b; // a=0, b=2, c=-1

(it is interpreted as a-- -b)

and this one isn't?

int c = a-----b;

The first statement could also be interpreted as a- --b, while the second statement clearly has only 1 logical interpretation which would be a-- - --b.

Also another curious one:

int c = a--- -b; // a=0, b=2, c=3

(and int c = a----b; isn't a legal statement)

How is the expression interpretation defined in Java? I tried searching JLS, but haven't found an answer for this.

2 Answers
Related