The code
int i = 0;
i = i++;
System.out.println(i);
produces the output "0" instead of "1". Why?
Let's take a close look at what the line "i = i++;" does:
- "i++" is evaluated. The value of "i++" is the value of i before the increment happens.
- As part of the evaluation of "i++", i is incremented by one. Now i has the value of 1;
- The assignment is executed. i is assigned the value of "i++", which is the value of i before the increment - that is, 0.
近期评论