According to http://introcs.cs.princeton.edu/java/11precedence/, post-increment operator has higher precedence than addition operator.
So, for the following code:
int i = 1;
int j = i + i++;
System.out.println(j);
I would have thought that the expression assigned to j would have been evaluated as follows (with each line being a "step" in the evaluation) :
i + i++
i + (1) // do post-increment operator; returns 1, and makes i = 2
(2) + (1) // do addition operator. need to get the operand i, so do that.
3
But when I try this program, the value of j is 2.
So I'm confused. In the expression, does it replace all the "i"s in the expression with the current value of i, BEFORE even touching the i++ ?
Edit: the phrase "evaluation order" that people here used, helped me to find the following potentially helpful stackoverflow answer: What are the rules for evaluation order in Java? .
Edit: I made my best guess into an answer below. I still welcome corrections to it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…