Alexander Zotz wrote:i thought the post-decrement would change the value after the calculation, like this:
2. 4 * 5 / 4
This is correct. The application prints "lion is 3" and "tiger is 5".
But in the explanation they do the post-decrement first.
The explanation is wrong.
Marc Fiore wrote:according to the java operator precedence rules, the postfix operator is evaluated first.
Incorrect. Operator precedence has nothing to do with order of evaluation. The only thing that operator precedence determines is which operand goes with which operator. It's as if the compiler inserts parentheses in the expression:
After this has been done, the expression is evaluated left to right, so (++lion * 5) is evaluated before (lion--).
Alexander Zotz wrote:OCP Java SE11 Complete study guide, site 86 is following Question:
i thought the post-decrement would change the value after the calculation, like this:
2. 4 * 5 / 4
after getting the result (2), i would decrement lion.
But in the explanation they do the post-decrement first. I cant understand why..
3. 3 * 5 / 3
Can someone explain me this?
In the numerator ++ has higher precedence so it increases the value of lion to 4, then multiplies. In the denominator, it divides with the incremented value of lion first and then decrements back to 4. Try changing it to --lion in the denominator;