有人可以解释一下Java的后期增量是如何工作的吗?

时间:2013-12-16 14:54:18

标签: java

有人可以解释一下Java的后期增量是如何工作的吗?

 public static void main(String[] args) {
            int a = 10;
            /*
             *  1. "a" is assigned to "b"
             *  2. Then a gets incremented by 1
             */
            int b = a++;
            System.out.println(b);  //prints 10
            System.out.println(a);  //prints 11


            int x = 10;
            /*
             *  1. "x" is assigned to "x"
             *  2. Then "x" is not getting incremented by 1
             */
            x = x++;
            System.out.println(x);  //prints 10
        }

因此,当我们在两边都有相同的变量时,结果会有所不同。请解释......

1 个答案:

答案 0 :(得分:0)

这是因为即使你增加x,你也要为x分配一个较旧的值(在这种情况下为10)