为什么a = a ++;不会改变a的值

时间:2019-03-20 16:10:34

标签: java

有人可以在这里向我解释一下堆栈操作

public static void main(String[] args) {
    int a=20;

    for(int i=1;i<10;i++) {
        a=a++;// why is the value of a unchanged here
        //System.out.println(i);
    }

    System.out.println(a);
}

1 个答案:

答案 0 :(得分:0)

前缀增量在变量增加后返回变量的值。 另一方面,后缀增量在变量增加之前返回变量的值。

这意味着当您说a = a ++时,它首先返回a的值,然后将其递增1。 因此,到行print(i)执行时,a就会增加1。

相关问题