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

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

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

16条回答
  •  别跟我提以往
    2020-11-22 03:17

    The Question is already answered, but allow me to add from my side too.

    First of all ++ means increment by one and -- means decrement by one.

    Now x++ means Increment x after this line and ++x means Increment x before this line.

    Check this Example

    class Example {
    public static void main (String args[]) {
          int x=17,a,b;
          a=x++;
          b=++x;
          System.out.println(“x=” + x +“a=” +a);
          System.out.println(“x=” + x + “b=” +b);
          a = x--;
          b = --x;
          System.out.println(“x=” + x + “a=” +a);
          System.out.println(“x=” + x + “b=” +b);
          }
    }
    

    It will give the following output:

    x=19 a=17
    x=19 b=19
    x=18 a=19
    x=17 b=17
    

提交回复
热议问题