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

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

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

16条回答
  •  一个人的身影
    2020-11-22 03:22

    There is a huge difference.

    As most of the answers have already pointed out the theory, I would like to point out an easy example:

    int x = 1;
    //would print 1 as first statement will x = x and then x will increase
    int x = x++;
    System.out.println(x);
    

    Now let's see ++x:

    int x = 1;
    //would print 2 as first statement will increment x and then x will be stored
    int x = ++x;
    System.out.println(x);
    

提交回复
热议问题