先上代码:
public class Demo {
public static void main(String[] args){
method_1();
method_2();
}
private static void method_1(){
int j = 0;
for(int i=0;i<100;i++){
j = j++;
}
System.out.println("method_1---"+j);
}
private static void method_2(){
int j = 0;
for(int i=0;i<100;i++){
j = ++j;
}
System.out.println("method_2---"+j);
}
}<strong>
</strong>
大家猜一下输出结果是什么?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
输出结果是:
method_1---0
method_2---100
为什么会是这样呢?
因为在计算过程中,使用了Java中间变量缓存机制。