Java guarantees ( ยง15.7.1 ) that it will be evaluated from left to right, giving 12. In particular, ++ has a higher priority than + . Therefore, he first links them, then compares the add operations from left to right
i = (((++i) + (++i)) + (++i));
In ยง15.7.1 it is said that the first operand is evaluated first, and ยง15.7.2 says that both operands are evaluated before the operation. Therefore, he evaluates as:
i = (((++i) + (++i)) + (++i)); i = ((3 + (++i)) + (++i)); // i = 3; i = ((3 + 4) + (++i)); // i = 4; i = (7 + (++i)); // i = 4; i = (7 + 5); // i = 5; i = 12;
In C, this behavior is undefined to change a variable twice without a sequence point between them.
Matthew flaschen
source share