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.

- 1

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.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

`i`, its value becomes 10. When you get to the second i with post-increment, the value 10 is used, not 9.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

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?

*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.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

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!

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

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.Dani Garcia wrote:thanks for that resource..it is wonderful!!!

We usually think the way Henry describes because that is how we were taught maths at school. Years of having BoDMAS drummed into us.