Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » OperatorsRSS Feeds

Increment and Decrement Operators

Posted By: Tami Romero     Category: Java     Views: 120537

This article lists and explains increment and decrement operators available in java.

Increment and decrement operators are used to perform increment or decrement variable value.

Increment and decrement operators with examples

Operator

Meaning and example 

1++ 

Post-increment adds 1 to the value. The value is returned before the increment is made. 

Example  a = 1;  b = a++; 

After execution of above statements value of variable a is 2 and value of b is 1.

1-- 

Post-decrement subtracts 1 from the value. The value is returned before the decrement is made.

Example a = 1; b = a--;

After execution of above statements value of variable a is 0 and value of b is 1.

++1 

Pre-increment adds 1 to the value. The value is returned after the increment is made.

Example a = 1; b = ++a;

After execution of above statements value of variable a is 2 and value of b is 2.

 --1

 Pre-decrement subtracts 1 from the value. The value is returned after the decrement is made.

Example a = 1; b = --a;

After execution of above statements value of variable a is 0 and value of b is 0.



Examples of Increment and Decrement Operators


Example 1 : Program that displays use of post-increment operator

class PostIncrOptDemo
{
  public static void main(String args[])
  {
int i = 5, j = 5, sum = 0;

System.out.println("Value of i is " + i + ", j is " + j " and sum is " + sum);
sum = i + j++;
System.out.println("Value of i is " + i + ", j is " + j " and sum is " + sum);
  }
}


Output

Value of i is 5, j is 5 and sum is 0
Value of i is 5, j is 6 and sum is 10

Increment of variable j takes place after value of variable j is returned to expression so it will return value 5 to the expression.


Example 2 : Program that displays use of post-decrement operator

class PostDecrOptDemo
{
  public static void main(String args[])
  {
int i = 5, j = 5, sum = 0;

System.out.println("Value of i is " + i + ", j is " + j " and sum is " + sum);
sum = i + j--;
System.out.println("Value of i is " + i + ", j is " + j " and sum is " + sum);
  }
}


Output

Value of i is 5, j is 5 and sum is 0
Value of i is 5, j is 4 and sum is 10

Decrement of variable j takes place after value of variable j is returned to expression so it will return value 5 to the expression.

Example 3 : Program that displays use of pre-increment operator

class PreIncrOptDemo
{
  public static void main(String args[])
  {
int i = 5, j = 5, sum = 0;

System.out.println("Value of i is " + i + ", j is " + j " and sum is " + sum);
sum = i + ++j;
System.out.println("Value of i is " + i + ", j is " + j " and sum is " + sum);
  }
}


Output

Value of i is 5, j is 5 and sum is 0
Value of i is 5, j is 6 and sum is 11

Increment of variable j takes place before value of variable j is returned to expression so it will return incremented value 6 to the expression.

Example 4 : Program that displays use of pre-decrement operator

class PreDecrOptDemo
{
  public static void main(String args[])
  {
int i = 5, j = 5, sum = 0;

System.out.println("Value of i is " + i + ", j is " + j " and sum is " + sum);
sum = i + --j;
System.out.println("Value of i is " + i + ", j is " + j " and sum is " + sum);
  }
}


Output

Value of i is 5, j is 5 and sum is 0
Value of i is 5, j is 4 and sum is 9

Decrement of variable j takes place before value of variable j is returned to expression so it will return decremented value 4 to the expression.
  
Share: 

 
 

Didn't find what you were looking for? Find more on Increment and Decrement Operators Or get search suggestion and latest updates.

Tami Romero
Tami Romero author of Increment and Decrement Operators is from Los Angeles, United States.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!