Skip to Main Content

New to Java

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!

Decrement operator precedence confusion

729894Feb 12 2010 — edited Feb 13 2010
Here is the snippet from a book:
int y = 5;
int result = y-- * 3 / --y;
System.out.println("y = " + y);
System.out.println("result = " + result);
Assuming increment and decrement have highest precedence, I don't get the explanation:
Order of operations dictates that the multiplication is evaluated first(WHY???). The value
of y is 5, so 5 * 3 is 15. The multiplication is done, so the post - decrement occurs and y
becomes 4. Now the division is evaluated and y is pre - decremented to 3 before the division,
resulting in 15 / 3 , which is 5. The output of this code is:

y = 3
result = 5

Any comments would be appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 13 2010
Added on Feb 12 2010
19 comments
260 views