aspose file tools*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes Operator Precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "Operator Precedence" Watch "Operator Precedence" New topic
Author

Operator Precedence

Brian O'Shea
Ranch Hand

Joined: Aug 30, 2012
Posts: 37
If postfix increment has a higher precedence than prefix increment and both work right to left why is y equal to 134.


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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

Joined: Aug 30, 2012
Posts: 37
Thanks Henry.

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

Joined: Sep 28, 2004
Posts: 18840
    
  40

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

Joined: Aug 30, 2012
Posts: 37
That postfix is higher than prefix.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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

Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 192
    
    1

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.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator Precedence