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

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

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

16条回答
  •  失恋的感觉
    2020-11-22 03:17

    These are known as postfix and prefix operators. Both will add 1 to the variable but there is a difference in the result of the statement.

    int x = 0;
    int y = 0;
    y = ++x;            // result: y=1, x=1
    
    int x = 0;
    int y = 0;
    y = x++;            // result: y=0, x=1
    

提交回复
热议问题