In this tutorial, we are going to see one of the most used and confused operators in java. That is increment and decrement operators.

increment and decrement operators :

Increment and decrement operators are unary operators. We can only apply these operators on a single operand, hence these operators are called as unary operators.

we can apply these unary operators on all primitive types except Boolean.

Increment operator (++):

the increment operator is an operator which is used to increase the value of a variable by 1, on which it is applied.

Again these increment operators are two types:

  • Pre increment (++x)
  • Post increment (x++)

Pre Increment Operator:

If an Increment operator is used in front of an operand, then it is called  as Pre Increment operator.

Syntax:

++x : which increments the value by 1 of ‘x’ variable.

Example:

class PreIncrement {
       
    public static void main(String[] args) {       
        int x = 10;       
        int y = ++x;       
        System.out.println("y value is: " + y);   
    }

}

Output:

y value is: 11
On the above example, pre increment operator is applied  on x operand, here first the value of x will be incremented by 1 and then the incremented value will be assigned to the variable y .
As per example, the initial value of ‘x’ is 10. After applying pre-increment operator on ‘x’ the value of ‘x’ is incremented by 1 (i.e., 11) and that value is assigned to the variable ‘y’. So that the final value of  ‘y’ is 11.

Post Increment Operator:

If an Increment operator is used after an operand, then is called Post Increment operator.

Syntax:

x++ : which increase the value by 1 of variable ‘x’.

Example:

class PostIncrement {

    public static void main(String[] args) {       
        int x = 10;       
        int y = x++;       
        System.out.println("y value is: " + y);   
    }

}

Output:

y value is: 10

Post increment operator is applied  on ‘x’, here the case is exact opposite of pre increment, first the value of variable ‘x’ is assigned to the variable ‘y’ and then the value of ‘x’ is incremented  by 1 .

As per example, the initial value of ‘x’ is 10. After applying post-increment operator the current values of ‘x’ (i.e, 10) is assigned to y, and then the value of ‘x’ is incremented by 1. So when displaying variable ‘y’ it is showing as 10.

Decrement Operator (- -):

The Decrement operator is an operator which is used to decrease the value of the variable by 1, on which it is applied.

Like increment operators, decrement operators are also 2 types,

  • Pre decrement (- -x)
  • Post decrement (x- -)

Pre Decrement Operator:

If a decrement operator is used in front of an operand, then it is called Pre decrement operator.

Syntax:

–x : which decrease the value by 1 of variable ‘x’ .

Example:

class PreDecrement {
    public static void main(String[] args) {
        int x = 10;
        int y = --x;
        System.out.println("y value is: " + y);
    }
}

Output:

y value is: 9

Pre decrement operator is applied on ‘x’, first, the value of ‘x’ will be decremented by 1 and then the decremented value will be assigned to the variable ‘y’.

As per example, the initial value of ‘x’ is 10. After applying pre decrement operator on ‘x’, the value of ‘x’ is decremented by 1 (i.e., 9) and that value is assigned to the variable ‘y’. So, when we display the variable ‘y’ it is showing as 9.

Post Decrement Operator:

If a decrement operator is used after an operand, then it is called Post decrement operator.

x- – : which decrease the value by 1 of variable ‘x’ .

Example:

class PostDecrement {
    public static void main(String[] args) {
        int x = 10;
        int y = x--;
        System.out.println("y value is: " + y);
    }
}

Output:

y value is: 10

Post decrement operator is applied on ‘x’, here the case is the complete opposite of pre decrement operator, first, the value of variable ‘x’ is assigned to the variable ‘y’ and then the value of ‘x’ is decremented by 1.
Syntax:
As per example, the initial value of ‘x’ is 10. After applying post decrement operator on variable ‘x’ the current values of ‘x’ (i.e, 10) is assigned to ‘y’, and then the value of ‘x’ is decremented by 1. So when displaying the value of ‘y’ it is showing as 10.

Limitations of Increment and Decrement Operators:

  • We can apply Increment and decrement operators only for variables but not for constant values. If we apply, then we will get compile time error.
class IncAndDecOperatorsDemo {

    public static void main(String[] args) {       
        int x = 10;       
        int y = ++10;       
        System.out.println("Value of y : " + y);

           
    }

}
Increment and decrement operators
  • We can’t apply the nesting on increment and decrement operators.
class IncAndDecOperatorsDemo {

    public static void main(String[] args) {       
        int x = 100;       
        int y = --(--x);       
        System.out.println("Value of y : " + y);

    }

}
Error
  • We can’t apply increment and decrement operators on final variables.
class IncAndDecOperatorsDemo {

    public static void main(String[] args) {       
        final int x = 100;       
        int y = ++x;       
        System.out.println("Value of y : " + y);

    }

}
Error2
  • We can’t apply increment and decrement operators on boolean types.
class IncAndDecOperatorsDemo {

    public static void main(String[] args) {
        boolean b = true;
        --b;
        System.out.println(b);

    }

}
Error3

Examples on Increment and Decrement Operators:

Example 1:

class IncrementOperator {

    public static void main(String[] args) {       
        int x = 10;       
        x = x++;       
        x = x++;       
        x = x++;       
        x = x++;       
        x = x++;       
        System.out.println("The Value of x is : " + x);

    }

}

Output:

Value of x : 10

The Step by Step Analysis on Output:

STEP 1 : Initial value of ‘x’ = 10;

STEP 2 : The value of ‘x’ is post incremented and assigned again to ‘x’.

The variable ‘x’ will be incremented first but the previous ‘x’ value (10) is assigned again to ‘x’ variable, and the incremented (11) value will be used after assigning. But in this example, the next value of ‘x’  is overridden by previous value (10) always.

STEP 3: The value of ‘x’ is post incremented and assigned to ‘x’ only.

STEP 4: The value of ‘x’ is post incremented and assigned to ‘x’ only.

STEP 5: The value of ‘x’ is post incremented and assigned to ‘x’ only.

Example 2:

class IncrementOperator {

    public static void main(String[] args) {       
        int x = 1;       
        x = x++ + ++x + x++ + ++x + ++x;       
        System.out.println("Value of x : " + x);

    }

}

Output:

Value of x : 18

The Step by Step Analysis on Output:

STEP 1 : initial ‘x’ value is 1

STEP 2 : x++ (1)

STEP 3 : ++x (2+1=3)

STEP 4 : x++ (3+1 (Post increment) =3)

STEP 5 : ++x (4+1 = 5)

STEP 6 : ++x (5+1=6)

STEP 7 : Add values from STEP 2 to STEP 6 (1+3+3+5+6)

STEP 8 : Answer is18

Happy Learning 🙂