Increment and Decrement Operators in Java

Increment and decrement operators in Java are also known as unary operators because they operate on a single operand. The increment operator (++) adds 1 to its operand and the decrement operator (–) subtracts one.

OperatorMeaning
++Increment Operator
Decrement Operator

Syntax:-

++variable;    // pre increment operator
variable++;    // post increment operator
--variable;    // pre decrement operator
variable--;    // post decrement operator

ExpressionThe initial
value of
a
The final
value of
a
Value
of
x
x = ++a455
x = a++454
x = –a433
x = a–434

Increment operators (++)

Pre-increment operators in Java (++a)

pre increment operator
  • First, the value of the variable a incremented by 1 and store in the memory location of variable a.
  • Second, the value of variable a assign to the variable x.
 class PreIncrementOperator{
   public static void main(String[] args) {
     int a, x;

     a = 10;
     x= ++a;

     System.out.println("a: "+a);
     System.out.println("x: "+x);
   }
 }

Output:-
a: 11
x: 11


Post-increment operators in Java (a++)

post increment operator
  • First, the value of the variable a will assign to the variable x.
  • Second, the value of the variable a will be incremented by 1 and store in the memory location of the variable a.
class PostIncrementOperator{
   public static void main(String[] args) {
     int a, x;
     a = 10;
     x = a++;
     System.out.println("a: "+a);
     System.out.println("x: "+x);
   }
 }

Output:-
a: 11
x: 10


Decrement Operators ( -- )

Pre-decrement operator in Java ( --a )

increment and decrement operators, pre decrement operator
  • First, the value of a decremented by 1 and store in the memory location of variable a.
  • Second, the value of the variable a assigned to the variable x.
class PreDecrementOperator{
   public static void main(String[] args) {
     int a, x;
     a = 10;
     x = --a;
     System.out.println("a: "+a);
     System.out.println("x: "+x);
   }
 }

Output:-
x: 9
y: 9


Post-decrement operator in Java ( a-- )

post decrement operator
  • First, value of variable a will assign to the variable x.
  • Second, the value of a decremented by 1 and store in the memory location of the variable a.
 class PostDecrementOperator{
   public static void main(String[] args) {
     int a, x;
     a = 10;
     x = a--;
     System.out.println("a: "+a);
     System.out.println("x: "+x);
   }
 }

Output:-
a: 9
x: 10


Limitations

The increment and decrement operators in Java can be applied to every primitive data type except boolean. But there are some limitations are there.

1) The Increment and decrement operators in Java only applied on variables (except final variables). It doesn’t work with constant values and final variables. If we try to use increment/decrement operators on constant values or final variables, then we will get a compile-time error.

10++; //Compile time error: unexpected type
++10; //Compile time error: unexpected type
 class Operators{
     public static void main(String[] args) {
         System.out.println(++9);
         System.out.println(9++);
     }
 }
> javac Operators.java
Operators.java:3: error: unexpected type
		System.out.println(++9);
		                     ^
  required: variable
  found:    value
Operators.java:4: error: unexpected type
		System.out.println(9++);
		                   ^
  required: variable
  found:    value
2 errors
 class FinalVariables{
     static final int a = 5;
     public static void main(String[] args) {
         System.out.println(++a);
         System.out.println(a++);
     }
 }
> javac Operators.java
Operators.java:4: error: cannot assign a value to final variable a
		System.out.println(++a);
		                     ^
Operators.java:5: error: cannot assign a value to final variable a
		System.out.println(a++);
		                   ^
2 errors

Final variables are also constant because after the initialization value of the final variable can’t be modified. So, increment/decrement doesn’t work with final variables.

2) Nesting of increment and decrement operators in Java is not allowed.

Guess the output of the below program.

 class Operators {
     public static void main(String[] args) {
         int a = 5;
         int x = ++ (++a);
         System.out.println(x);
     }
 }

We will get a compile-time error because the nesting of the increment and decrement operators is not allowed. In this program, due to ++a the value becomes 6, and now the operation is int x=++6; which is invalid.

3) Increment and decrement operators in Java don’t work on boolean and String data types.

boolean bo = true;
bo++; //compile time error:- bad operans boolean

String s1 = “a”;
s1++; //Compile time error:- bad operans String

4) The Increment and decrement operators also doesn’t work on an object reference.

 class A{}

 class IncDecOperator{
   public static void main(String[] args) {

     A a1 = new A();
     a1++;

     int[] ia = new int[5];
     ia++;
   }
 }
IncDecOperator.java:5: error: bad operand type A for unary operator '++'
    a1++;
      ^
IncDecOperator.java:8: error: bad operand type int[] for unary operator '++'
    ia++;
      ^
2 errors

Some Important points on increment and decrement operators in Java

After increment and decrement, the data type of variable doesn’t change. For example:- If the variable was char data type then after increment/decrement it remains char data type.

 class Operators {
     public static void main(String[] args) {
         char ch = 'x';
         System.out.println(++ch);
     }
 }

Output:-
y

 class Operators {
     public static void main(String[] args) {
         double a = 5.9;
         System.out.println(++a);
     }
 }

Output:-
6.9

 class IncDecOperator{
   public static void main(String[] args) {

     int x = 10;
     x++;
     System.out.println("x++: "+x);
     ++x;
     System.out.println("++x: "+x);

     long l = 20;
     l++;
     System.out.println("l++: "+l);

     float f = 50.0F;
     ++f;
     System.out.println("++f: "+f);

     double d = 9.0;
     d++;
     System.out.println("d++: "+d);

     char ch = 'a';
     ch++; //Or, ch = (char)(ch+1)
     System.out.println("ch++: "+ch);

     byte b = 1;
     ++b; //Or, b = (byte)(b+1)
     System.out.println("++b: "+b);

     b = 127;
     b++; //Or, b = (byte)(b+1)
     System.out.println("b: "+b);
   }
 }

Output:-
x++: 11
++x: 12
l++: 21
++f: 51.0
d++: 10.0
ch++: b
++b: 2
b: -128


Increment and decrement operators in Java exercises

Find out the output of the below programs?

Program1

class PostOperator{
   public static void main(String[] args) {

     int x = 10;
     x++;
     System.out.println("x: "+x);

     int y = 10;
     y--;
     System.out.println("y: "+y);

   }
 }

Output:-
x: 11
y: 9


Program2

 class IncrementOperator1{
   public static void main(String[] args) {

     int x = 1;
     System.out.println("Hi-"+ x++);
     System.out.println("x: "+ x);

     x = 1;
     System.out.println("Hi-"+ ++x);
     System.out.println("x: "+ x);
   }
 }

Output:-
Hi-1
x: 2
Hi-2
x: 2


Program3

 class IncrementOperator2{
   public static void main(String[] args) {

     int x = 10;
     int y = ++x;
     System.out.println("x: "+ x);
     System.out.println("y: "+ y);

     x = 10;
     y = x++;
     System.out.println("x: "+ x);
     System.out.println("y: "+ y);
   }
 }

Output:-
x: 11
y: 11
x: 11
y: 10


Program4

 class IncrementOperator3{
   public static void main(String[] args) {

     int x = 10;
     x = ++x;
     System.out.println("x: "+ x);

     x = 10;
     x = x++;
     System.out.println("x: "+ x);
   }
 }

 

Output:-
x: 11
x: 10

 


Program5

 class IncDecOperator{
   public static void main(String[] args) {

     int x = 1;
     int y = x++ + ++x;
     System.out.println("x: "+ x);
     System.out.println("y: "+ y);
     System.out.println();

     x = 1;
     y = x++ + ++x + x++ + x-- + --x + ++x;
     System.out.println("x: "+ x);
     System.out.println("y: "+ y);
     System.out.println();

     x=1;
     y = ++x + --x - x-- + x++ - ++x;
     System.out.println("x: "+ x);
     System.out.println("y: "+ y);
   }
 }

 

Output:-
x: 3
y: 4

x: 3
y: 16

x: 2
y: 0

 

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *