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

Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
Hi,

I would like to ask if somebody can clarify me the output of the followed codes.

**********

1. since pos increment has precedence over preincrement, i++ is evaluted first, so the expresion is "++i - y + 9"
2. i was incremented and since preincrement has precende over the rest operators, the expresion is "11 - y + 9 "
3. so the final expresion would "11 - 2 + 9" = 18, that is what I expect

**********
but I dont understand this output

1. for me, since postincrement has precedence over preincrement, i++ is evaluted first so the expresion is "++i * y + 9"
2. i was incremented and since preincrement has precende over the rest operators, the expresion is "11 * y + 9 "
3. so the final expresion would be "11 * 2 + 9" = 31, but it is 30 can somebody clarify why this?

Thanks.

Sheriff
Posts: 17651
300
• 1
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch!

The fact that post-increment has precedence over pre-increment has no bearing here. It's more to do with left to right evaluation of expressions. The expression evaluates to 10 - 2 + 10, not 11 - 2 + 9. Unfortunately, both are equal to 18 which led you to think you were correct in thinking how it evaluated.

Likewise, the second expression evaluates to 10 * 2 + 10, which of course is 30 not 31.

Junilu Lacar
Sheriff
Posts: 17651
300
• Number of slices to send:
Optional 'thank-you' note:
In both your #1 points, your interpretation of how operator precedence works is incorrect. Think of operator precedence as putting explicit parentheses around the terms of an expression. After explicit parentheses have been added, evaluation of the expression still proceeds from left to right. The effects of pre-increment operations are applied as you go. So, in the first pre-increment of the variable i, its value becomes 10. When you get to the second i with post-increment, the value 10 is used, not 9.

Dani Garcia
Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:

Junilu Lacar wrote:In both your #1 points, your interpretation of how operator precedence works is incorrect. Think of operator precedence as putting explicit parentheses around the terms of an expression. After explicit parentheses have been added, evaluation of the expression still proceeds from left to right. The effects of pre-increment operations are applied as you go. So, in the first pre-increment of the variable i, its value becomes 10. When you get to the second i with post-increment, the value 10 is used, not 9.

Thanks for your answer Junilu. I thought postincrement has precende over preincrement because under https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html postfix is before unary. Am I interpreting this table wrong?

Junilu Lacar
Sheriff
Posts: 17651
300
• Number of slices to send:
Optional 'thank-you' note:
Yes, you are misinterpreting it. The precedence applies to operator evaluation, not expression evaluation. Consider this expression:

1 + 2 * 3

This expression is evaluated as 1 + (2 * 3), which results in the value 7, not 9. Now, if we had this code:

The above code will print out 7 because it evaluates to 1 + 2 * 3. So, even though unary pre-increment has a higher precedence over * which has a higher precedence over +, the evaluation still proceeds from left to right and the pre-incrementing of i happens as the variable i is used throughout the expression. I hope that makes sense.

Dani Garcia
Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
Oh! Thanks for your aclaration. It has made me to see the difference. Now I understand what you meant with the brackets. I have been reading about expresion and operators, and I found a comment that helped me too.

Consider A() + B() + C() * D(). Multiplication is higher precedence than addition, and addition is left-associative, so this is equivalent to (A() + B()) + (C() * D()) But knowing that only tells you that the first addition will happen before the second addition, and that the multiplication will happen before the second addition. It does not tell you in what order A(), B(), C() and D() will be called!

Ranch Hand
Posts: 491
23
• 1
• Number of slices to send:
Optional 'thank-you' note:
you can have a great understanding of expressions(even other things)→here.

Kind regards,
praveen.

Dani Garcia
Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
thanks for that resource..it is wonderful!!!

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

Junilu Lacar wrote:Consider this expression:

1 + 2 * 3

This expression is evaluated as 1 + (2 * 3), which results in the value 7, not 9.

I think the other issue is human nature -- as the reason why many of us confuse precedence with evaluation order. It is in our nature to work "outward" when evaluating an expression like "1 + (2 * 3)", ie. doing the multiplication first.  People forget that computers are different, and tend to do stuff based on how they are programmed (and specified) to do it.

And with Java, expressions are done left to right (using temporary/stack variables when needed).

Henry

Marshal
Posts: 79273
377
• Number of slices to send:
Optional 'thank-you' note:

Dani Garcia wrote:thanks for that resource..it is wonderful!!!

It is good; there are a few tiny errors, e.g. saying that + is overloaded when both its operands are String type. Since the article mentions the Math class' exact methods, it appears to be reasonably recent.
We usually think the way Henry describes because that is how we were taught maths at school. Years of having BoDMAS drummed into us.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.