"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.
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
Thanks and Regards,
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.
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.
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.