java虚拟机的机制与C的不同,同样的程序在C语言中 如下:- #include<stdio.h>
- void main()
- {
- int i; int j=0;
- for(i=0;i<100;i++)
- j=j++;
- printf("%d",j);
- }
复制代码 结果为100
我查阅了有关资料 得出下面的回答
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 = 0;
x = x++;
Steps:
1 initial value of x is stored in temp register. So temp = 0.
2 x is incremented. x = 2 and temp = 0.
3 the value of the temp register is assigned to the LHS. x = 0
我翻译下,
程序运行中,x的起始值被存在了一个暂时的寄存器中,然后那个原始的x自增1,然后寄存器中的x值给了表达式的等号左边,所以x的值总是原始的。
如果楼主想进一步研究,可以借助反编译,有下面的链接
http://zhidao.baidu.com/question/392618024.html |