File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

postfix and prefix increment precedence in conditional expressions

 
Greg Stevens
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,

 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you have been given a correct explanation, but I hope this will teach you not to write illegible code.
 
Greg Stevens
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to tell us precedence and evaluation order are not the same thing, Henry Wong?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry - I'm sorry, but you're wrong.

Precedence and Evaluation Order are not the same thing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic