int i = 1;
System.out.println(i++ + ++i);
Why is this 4 given the following:
- Expressions in Java are evaluated from left to right (after precedence)
- Post increment (i++) has higher precedence. So it should be 1 + 2.
I recommend deleting the question by a moderator, since the answer is simply (as per Joachim's comment) that the evaluation of expression in context of post increment operator includes any expression within the expression and not just the top one of the primary statement (sysout in this case). Given i++ is a statement in itself which increments a variable by 1 it gets already executed in sub-expression (i++) but returns i to parent expression. that was simply what I was not aware of.