Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JAVA operator precedence table is wrong.

807606May 12 2007 — edited Jun 3 2007
I 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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 1 2007
Added on May 12 2007
30 comments
880 views