What is x after “x = x++”?

后端 未结 17 1323
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 06:26

What happens (behind the curtains) when this is executed?

int x = 7;
x = x++;

That is, when a variable is post incremented and assigned to

17条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-21 06:41

    When you re-assign the value for x it is still 7. Try x = ++x and you will get 8 else do

    x++; // don't re-assign, just increment
    System.out.println(x); // prints 8
    

提交回复
热议问题