Java increment & decrement operator
Increment and Decrement Operator in Java :
- It is one of the variation of “Arithmetic Operator“.
- Increment and Decrement Operators are Unary Operators.
- Unary Operator Operates on One Operand.
- Increment Operator is Used to Increment Value Stored inside Variable on which it is operating.
- Decrement Operator is used to decrement value of Variable by 1 (default).
Types of Increment and Decrement Operator :
- Pre Increment / Pre Decrement Operator
- Post Increment / Post Decrement Operator
Syntax :
++ Increment operator : increments a value by 1 -- Decrement operator : decrements a value by 1
Pre-Increment Operator :
- “++” is written before Variable name.
- Value is Incremented First and then incremented value is used in expression.
- “++” cannot be used over “Constant” of “final Variable“.
Live Example 1 : Post Incrementing Variable
[468x60]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
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[468x60]
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); } }
- If Inside println we have post increment then firstly Old value gets printed on screen and after printing variable gets new value.
- 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.