Increment/Decrement Operators

Learn how to use the pre-increment (++i), post-increment (i++), pre-decrement (--i), and post-decrement (i--) operators in Java.

Java · beginner · 15 min · 18 steps · free

What you'll learn

IncrementDecrementDemo.java

public class IncrementDecrementDemo {
    public static void main(String[] args) {
        // Post-increment (a++)
        int a = 5;
        System.out.println("Initial a: " + a);
        int b = a++; // b gets the original value of a, then a is incremented
        System.out.println("After a++:");
        System.out.println("a is " + a);
        System.out.println("b is " + b);
        // Pre-increment (++c)
        int c = 5;
        System.out.println("Initial c: " + c);
        int d = ++c; // c is incremented first, then d gets the new value of c
        System.out.println("After ++c:");
        System.out.println("c is " + c);
        System.out.println("d is " + d);

        // Post-decrement (x--)
        int x = 10;
        System.out.println("Initial x: " + x);
        int y = x--; // y gets the original value of x, then x is decremented
        System.out.println("After x--:");
        System.out.println("x is " + x);
        System.out.println("y is " + y);

        // Pre-decrement (--z)
        // Same logic decreases the value and then assign the decreased value
        // int z = 10;
        // System.out.println("Initial z: " + z);
        // int w = --z; // z is decremented first, then w gets the new value of z
        // System.out.println("After --z:");
        // System.out.println("z is " + z);
        // System.out.println("w is " + w);
        // result will be ->
        // z is 9
        // w is 9
    }
}

Step by step

  1. We declare an integer variable a and initialize it with the value 5.
  2. The current value of a is printed to the console.
  3. Post-increment (a++) is used. First, the original value of a (5) is assigned to b.
  4. After the assignment, a is incremented. Its value changes from 5 to 6.
  5. The new value of a (which is 6) is printed.
  6. The value of b (which is 5) is printed. Note how it holds the value of a before the increment.
  7. Now let's see pre-increment. We initialize c to 5.
  8. Now let's see pre-increment. We initialize c to 5.
  9. Pre-increment (++c) is used. First, c is incremented. Its value changes from 5 to 6.
  10. After the increment, the new value of c (6) is assigned to d.
  11. The value of c (now 6) is printed.
  12. The value of d is also 6. Note how both c and d have the same value after the operation.
  13. The same principles apply to decrement operators. We initialize x to 10.
  14. With post-decrement (x--), the original value of x (10) is assigned to y. Then x becomes 9.
  15. With post-decrement (x--), the original value of x (10) is assigned to y. Then x becomes 9.
  16. With post-decrement (x--), the original value of x (10) is assigned to y. Then x becomes 9.
  17. With post-decrement (x--), the original value of x (10) is assigned to y. Then x becomes 9.
  18. With post-decrement (x--), the original value of x (10) is assigned to y. Then x becomes 9.

Common mistakes

Best practices

·

More in Operators