JAVA operator precedence table is wrong.
807606May 12 2007 — edited Jun 3 2007I can not understand why JAVA auto-increment/decrement operator does not follow the rules of Operator Precedence table at http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
The precedent order is as follow:
postfix: expr++ expr--
unary: ++expr --expr +expr -expr ~ !
multiplicative: * / %
From the above we know that x++/x-- is higher then ++x/--x and multiplicative is the lower then both.
I tested the following in JAVA:
int y,x = 2;
y = --x * x++;
System.out.println(x); //Output is 1.
Why?
Theoretically, x++ must be performed before --x, finally then the multiplication (*) and the answer should be
x++ value=2, stored variable=3
--x value=2, stored variable=2
Therefore 2 * 2 = 4.
Why JAVA is not following the rules that it setup for itself??
After much research, my conclusion is JAVA Operator Precedence table in java.sun.com website is wrong. The right version should be the same as this website: http://www.sorcon.com/java2/precedence.htm.
Please run your example code in JAVA before you want to prove me wrong.
Thank you.