Is there a difference between x++ and ++x in java?

后端 未结 16 2422
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:34

Is there a difference between ++x and x++ in java?

16条回答
  •  礼貌的吻别
    2020-11-22 03:22

    Yes, the value returned is the value after and before the incrementation, respectively.

    class Foo {
        public static void main(String args[]) {
            int x = 1;
            int a = x++;
            System.out.println("a is now " + a);
            x = 1;
            a = ++x;
            System.out.println("a is now " + a);
        }
    }
    
    $ java Foo
    a is now 1
    a is now 2
    

提交回复
热议问题