• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator Precedence

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If postfix increment has a higher precedence than prefix increment and both work right to left why is y equal to 134.


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian O'Shea wrote:If postfix increment has a higher precedence than prefix increment and both work right to left why is y equal to 134.



Not sure of the "higher precedence" or the "right to left" argument. There is no simultaneous usage of prefix and postfix, where you need to determine operator order (precedence or associativity).

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian O'Shea wrote:If postfix increment has a higher precedence than prefix increment and both work right to left why is y equal to 134.



First, apply precedence -- and you get...



Note that it doesn't matter that postfix has higher precedence than prefix. And associativity is not needed.

To evaluate, you need to go left to right -- as defined by the specification. Normally, this is not a concern, but since prefix and postfix has side effects, you have to evaluate in the correct order. Also, since there are no short-circuiting operators being used, no need to deal with the added complexity. So...



Henry
 
Brian O'Shea
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry.

I was looking at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html and that seemed strange.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian O'Shea wrote:Thanks Henry.

I was looking at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html and that seemed strange.




What is strange about the operator precedence chart?

Henry
 
Brian O'Shea
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That postfix is higher than prefix.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brian O'Shea wrote:That postfix is higher than prefix.



Does it really matter? Since it is not possible to apply both postfix and prefix at the same time, any difference in precedence is moot (as it is a syntax error). The precedence of postfix over the unary operators are more applied to the other unary operations (than the prefix operators).

Now, as for why the prefix operator has the same precedence as the unary operators -- I never really thought about it much. My guess is that it works better that way. Regardless, it is the same precedence of C/C++, which Java heavily borrowed from. Perhaps the Java designers didn't think about it much either ...

Henry

 
Ranch Hand
Posts: 201
1
Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Operator precedence can be confusing

For post-increment and pre-increment I would just remember that pre-increment operators change the value before the expression is evaluated and post-increment changes it after.

So for instance given this code:


What is happening is java interprets this as: y = (11 * 11) + 13;
This is because 10 is first pre-incremented to 11 then because of precedence the multiplication part of the expression happens first. Now keep in mind that the post-increment on the second operand has not executed yet, it does this sometime before the last addition operator executes so 11 post-increments to 12 for instance then pre-increments to 13. And there you have it!

Check out this picture I made to help memorize java operator precedence.


 
reply
    Bookmark Topic Watch Topic
  • New Topic