Java代码:- int j = 0;
- int i = 0;
- for(i=0; i<100; i++) {
- j = j++;
- }
- System.out.println("j: " + j);
复制代码 C代码:- int j = 0;
- int i = 0;
- for(i=0; i<100; i++)
- {
- j = j++;
- }
- printf("%d\n", j);
复制代码 同一段代码 Java输出的结果0,但C输出的却是100呢? 究其原因是 两种语言的编译器不同
关于java代码最终的解释:
What happens is that the initial value of x is stored in a temporary register, then x is incremented, then the value stored in the register is asigned to the left hand side of the expression, in this case the LHS is x so x gets its original value.
int x = 1;
x = x++;
Steps:
1 initial value of x is stored in temp register. So temp = 1.
2 x is incremented. x = 2 and temp = 1.
3 the value of the temp register is assigned to the LHS. x = 1
|