Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes postfix and prefix increment precedence in conditional expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "postfix and prefix increment precedence in conditional expressions" Watch "postfix and prefix increment precedence in conditional expressions" New topic
Author

postfix and prefix increment precedence in conditional expressions

Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
I am confused with the following code:


Here is the output:


"Conditional 2" and "conditional 4" do not behave as I would expect according to the operator precedence
rules. If the unary operators have higher precedence than the equality operators, then why aren't num++
and ++num evaluated first when they are on the right side of the == operator?

I am especially confused by this because the Java Language Specification says:
15.7.1 Evaluate Left-Hand Operand First
The left-hand operand of a binary operator appears to be fully evaluated before
any part of the right-hand operand is evaluated.


I'm sure I'm probably taking this out of context, but in "conditional 2" and "conditional 4", the left-hand
operands certainly seem to be evaluated first. If left-hand operands of binary operators are evaluated
first, what is the point of the higher precedence of unary operators? Are the unary operators applied
first seperately on each side of the binary operator, recusrively in some sort of tree structure. That seems
rediculously confusing. I know this is supposed to be very simple, but I can't seem to get past the apparent
contradiction. Thanks in advance for any help.
Suhas Bilaye
Ranch Hand

Joined: Sep 10, 2009
Posts: 80
Hi,



In conditional 2,
Left hand side is evaluated first and then the right hand side is evaluated then the equality operator checks for equality
postfix operator first returns the value of num i.e 10 then the equality operator runs
before the equality operator runs the value is incremented but the value returned is the 10



In conditional 4,
Same : Left hand side is evaluated first and then the right hand side is evaluated then the equality operator checks for equality.
But the prefix operator first increments the value and returns it so ++num evaluates to 11 and then equality operator returns false.

Basically the precedence is still maintained but the behaviour of prefix and postfix;
says that postfix returns the original value first which is used in the evaluation of the expression and then increments and prefix increments the value first and then the new value is used in the expression.

I hope this helps

Regards,


Thanks and Regards,
Suhas
http://www.xplore-java.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Yes, you have been given a correct explanation, but I hope this will teach you not to write illegible code.
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
Thank you for walking through the examples. What I don't understand is what does it mean for the unary operators to have higher
precedence than the equality operators if the are not evaluated first.

In other words

num = 10
num == num++

shouldn't num++ be evaluated first (evaluating to 10, but incrementing num to 11), then when num on the left is evaluated it
would be 11. 11 == 10: false
Greg Stevens
Ranch Hand

Joined: Jul 23, 2009
Posts: 41
but I hope this will teach you not to write illegible code


I would never write code like that. I have definitely learned not to use pre or post increment in an expression in which the variable occurs
more than once.

I see that the compiler gives the answer it does. Actually, my problem is that that I am studying in a class and the instructor is teaching that
the rules of precedence apply this way (obviously incorrect). I think he is confused. I am trying to figure out what is correct.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Operators are evaluated from left to right. Look at this Java™ Language Specification section. Note it says:
The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
Your example has num which is 10 to the left of the ==, then you go to the ==, but before the == is evaluated you execute the ++. Now num++ is still 10, so it returns true. It doesn't go back to the num with no operator on the left and alter that. That would be right-to-left evaluation.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

Greg Stevens wrote:Thank you for walking through the examples. What I don't understand is what does it mean for the unary operators to have higher
precedence than the equality operators if the are not evaluated first.

In other words

num = 10
num == num++

shouldn't num++ be evaluated first (evaluating to 10, but incrementing num to 11), then when num on the left is evaluated it
would be 11. 11 == 10: false


Precedence and Evaluation Order are not the same thing. Precedence and Evaluation Order are not the same thing. Precedence and Evaluation Order are not the same thing. See my explanation in your other topic.

http://www.coderanch.com/t/481607/Beginning-Java/java/yet-another-postfix-prefix-precedence

And BTW, did I also mentioned that Precedence and Evaluation Order are not the same thing?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
Are you trying to tell us precedence and evaluation order are not the same thing, Henry Wong?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Henry - I'm sorry, but you're wrong.

Precedence and Evaluation Order are not the same thing.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: postfix and prefix increment precedence in conditional expressions