According to the Oracle operator precedence specification, an operation such as:
x-- // Returns x, then subtracts 1 from x.
should take precedence over:
--x // Subtracts 1 from x, then returns x.
So, given the small snippet below, why does:
int x = 5;
int y = --x * 5 + x--;
System.out.println(x+" vs "+y);
print 3 vs 24
instead of 3 vs 20
?
Elaboration
Assuming the given order of operator precedence, one could break down the line #2 of the snippet to the following pseudo-blocks (previously evaluated values put in square brackets):
Evaluate
x--
Evaluate
--x
Evaluate
[--x] * 5
Evaluate
[--x * 5] + [x--]
Evaluate
y = [--x * 5 + x--]
Which would then resolve as follows:
After 1 returns 5
sets x to 4
After 2 sets x to 3
returns 3
After 3 multiplies 3 by 5 and returns 15
After 4 adds 15 to 5 and returns 20
After 5 sets y to 20
How come the returned value is 24 not 20.
P.S. (You get 24 if you evaluate --x before x-- but it should not be the case due to operator precedence).
Am I blind or just bad at math or what?