Java increment & decrement operator



Increment and Decrement Operator in Java :

  1. It is one of the variation of “Arithmetic Operator“.
  2. Increment and Decrement Operators are Unary Operators.
  3. Unary Operator Operates on One Operand.
  4. Increment Operator is Used to Increment Value Stored inside Variable on which it is operating.
  5. Decrement Operator is used to decrement value of Variable by 1 (default).

Types of Increment and Decrement Operator :

  1. Pre Increment / Pre Decrement Operator
  2. Post Increment / Post Decrement Operator

Syntax :

++ Increment operator : increments a value by 1
-- Decrement operator : decrements a value by 1

Pre-Increment Operator :

  1. “++” is written before Variable name.
  2. Value is Incremented First and then incremented value is used in expression.
  3. “++” cannot be used over “Constant” of “final Variable“.

Live Example 1 : Post Incrementing Variable

[468×60]

class PostIncrement {
  public static void main(String args[]) {
    int num1 = 1;
    int num2 = 1;

    num1++;
    num2++;

    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);
  }
}

Output :

num1 = 2
num2 = 2
  • Post Increment : Increment Value of Variable After Assigning
  • num1++ will increment value inside “num1” variable after assigning old value to itself.
  • New Value of num1 = 2.

Live Example 2 : Pre Incrementing Variable

class PreIncrement {
  public static void main(String args[]) {
    int num1 = 1;
    int num2 = 1;

    --num1;
    --num2;

    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);
  }
}

Output :

num1 = 0
num2 = 0
  • Pre Increment : First Increment Value and then Assign Value.
  • ++num1 will increment value inside “num1” variable. New value is assigned to itself.
  • New Value of num1 = 0.

Live Example 3 : Post Incrementing Variable

Increment and Decrement in Java

class PreIncrement {
  public static void main(String args[]) {
    int num1;
    int num2;
    int num3;

    num1 = 100;
    num2 = ++num1;
    num3 = num2++ + ++num1;

    System.out.println("num1 = " + num1);
    System.out.println("num2 = " + num2);
    System.out.println("num3 = " + num3);
  }
}

Explanation :

  • Initially num1 = 100.

Consider this Expression –

num2 = ++num1;
  • Value is Assigned to Variable Present at Left hand Side, just after incrementing  “num1” .
Step 1 : Increment Value of num1.
Step 2 : New Value of num1 = 101
Step 3 : Assign Value of num1 to num2
Step 4 : New Value of num2 = 101

Consider this Expression –

num3 = num2++ + ++num1;
  • During Expression evaluation only pre incremented value is used. (post increment operator is used just after completion of expression evaluation)
Step 0 : Initial Values -
         num1 = 101
         num2 = 101
Step 1 : Increment Value of num1 as it is (pre-increment)
Step 2 : Keep Value of num2 as it is (post-increment)
Step 3 : At this stage -
         num1 = 102
         num2 = 101
Step 4 : Add num1 & num2
         num3 = Old(num2) + new(num1)
         num3 = 101 + 102
         num3 = 203
Step 5 : Expression Completed
         num3 = 203
Step 6 : Increment Value of num2 (pending post-increment)
         num2 = 102
Step 7 : Final Values
         num1 = 102
         num2 = 102
         num3 = 203

[468×60]

Live Example 4 : Incrementing Variable inside Println Method

class PrePostDemo {
    public static void main(String[] args){
        int i = 3;
        i++;

        // prints 4
        System.out.println(i);
        ++i;

        // prints 5
        System.out.println(i);

        // prints 6
        System.out.println(++i);

        // prints 6
        System.out.println(i++);

        // prints 7
        System.out.println(i);
    }
}
  1. If Inside println we have post increment then firstly Old value gets printed on screen and after printing variable gets new value.
  2. In case of pre increment Variable gets new value by executing increment operation and new value will be printed on the screen.

Live Example 5 : Yet another Example of Increment Operator

class IncDec {
  public static void main(String args[]) {
    int a = 1;
    int b = 2;
    int c;
    int d;
    c = ++b;
    d = a++;
    c++;
    System.out.println("a = " + a);
    System.out.println("b = " + b);
    System.out.println("c = " + c);
    System.out.println("d = " + d);
  }
}

Output :

a = 2
b = 3
c = 4
d = 1

Live Example 6 : Incrementing Final Value

class IncrementFinal {

    public static void main(String[] args){

        final int constVal = 10;
        System.out.println(constVal);
    }
}

Output :

Compile Time Exception will be thrown
  • We cannot increment or decrement Constant Value in Java.
  • Constant Value in Java are nothing but final values.