1

I have trouble understanding operator precedence in post increment and pre increment operators. I did my research and found below article in StackOverFlow

Precedence of ++ and -- operators in Java

But it does not answer my questions.

In the code below

d = 1; System.out.println(++d * d++);

According to one of the answer by @PaĆ­lo Ebermann the above expression is evaluated as ++d * d++ => (++d) * (d++) and then evaluated from left to right which gives answer as 2*2=4 which is ofcourse what the java says when I print it.

My understanding is I believe that the expression is evaluated as ++d * d++ => (++d * (d++)) since d++ has higher precedence than ++d which gives answer as 3*1=3.

EDIT: Another example to support my claim- Consider expression 2+3*5 which gets evaluated to (2+(3*5)) and evaluation happens in that manner(Multiplication followed by addition). But consider this expression int i=1;sysout(i+i++); Here i+i++ must change to (i+(i++)). The post increment must increase i by two(similar to multiplication) which is assigned to i which makes 2+1=3 but the output is 2.

Please clarify what I am thinking wrong here in relation to precedence to evaluation order

2 Answers2

2

Precedence only appear when there is a conflict on operants. For 3 + 7 * 9, both + and * are binary operator, having a conflict because they both they act on the same operand 7. According to the precedence order, it equals to 3 + (7 * 9).

But in your case ++d * d++, the pre-increment and post-increment are unary operaters. They act on different variables d, not having a conflict or share a common operand. So the higher precedence rule doesn't work here. Just calculate with left to right order.

Refer this website for more details about the conception of Precedence order and Associativity and Order of operand evaluation. https://introcs.cs.princeton.edu/java/11precedence/

1

You are mixing up operator precedence with evaluation of subexpressions. Operator precedence only tells us how to add the missing parentheses. Once an expression has been fully parenthesised so that the underlying tree structure is obvious, we can forget about operator precedence.

When evaluating a fully parenthesised expression such as (++d) * (d++), we start with the outermost operator, which in this case is multiplication. This outermost operator connects several subexpressions, which we need to evaluate (recursively, i.e. following the same principles as for the complete expression) in order from left to right. At this point, operator precedence is no longer relevant. In particular, it is not true that in the special case of the operator(s) in the right expression having higher precedence than that/those in the left expression, the right expression is evaluated first. Subexpressions are strictly evaluated from left to right in all cases.

(Strictly speaking, this is not exactly true. Only the result must be as if this were true. I guess this is relevant primarily in connection with multithreaded code, where another thread might interrupt ours in the middle of the calculation and notice that a side effect of the right expression has occurred already, but not yet that of the left expression.)