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

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

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

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 03:37

    Yes, there is a difference, incase of x++(postincrement), value of x will be used in the expression and x will be incremented by 1 after the expression has been evaluated, on the other hand ++x(preincrement), x+1 will be used in the expression. Take an example:

    public static void main(String args[])
    {
        int i , j , k = 0;
        j = k++; // Value of j is 0
        i = ++j; // Value of i becomes 1
        k = i++; // Value of k is 1
        System.out.println(k);  
    }
    

提交回复
热议问题