x=x++问题

下面代码

public class Test{
    public static void main(){
        int x=0;
        for(int i = 0;i<100;i++)
            x=x++;
        System.out.println(x);
    }
}

运行结果为:0

原因:
对于x=x++,
x++执行后,将旧值赋给x,
故x一直为0。

即x=x++;相当于以下代码:

int t = x;
x++;
x = t;

猜你喜欢

转载自blog.csdn.net/qq_41135704/article/details/81590362