Facebook Page Twitter Page LinkedIn Page
× Java Tutorial Java Advanced Java Resources


The increment (++) is an unary operator in Java and hence acts upon a single operand to produce a new value. It has two variant:

  • Pre-increment: Increases the value of the operand by 1, then returns the operand.
  • Post-increment: Returns the operand, then increases the value of the operand by 1.

Example: Pre-increment operator

The below example describes the usage of pre-increment operator.

public class MyClass {
  public static void main(String[] args) {
    int x = 10;
    int y = 20;
    int z;

    //below expression is equivalent to
    //x = x + 1; z = x + y;
    z = ++x + y;    

    //Displaying the result
    System.out.println("x = "+ x);
    System.out.println("y = "+ y);
    System.out.println("z = "+ z);
  }
}

The output of the above code will be:

x = 11
y = 20
z = 31

Example: Post-increment operator

The below example describes the usage of post-increment operator.

public class MyClass {
  public static void main(String[] args) {
    int x = 10;
    int y = 20;
    int z;

    //below expression is equivalent to
    //z = x + y; x = x + 1; 
    z = x++ + y;    

    //Displaying the result
    System.out.println("x = "+ x);
    System.out.println("y = "+ y);
    System.out.println("z = "+ z);
  }
}

The output of the above code will be:

x = 11
y = 20
z = 30

❮ Java - Operators