Search code examples
javasyntaxoperatorsincrement

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


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


Solution

  • ++x is called preincrement while x++ is called postincrement.

    int x = 5, y = 5;
    
    System.out.println(++x); // outputs 6
    System.out.println(x); // outputs 6
    
    System.out.println(y++); // outputs 5
    System.out.println(y); // outputs 6