Increment (++) and Decrement (--) Operators - Java Language Basics

Java examples for Language Basics:Operator

Introduction

There are two kinds of increment operators:

  • Post-fix increment operator, for example, i++
  • Pre-fix increment operator, for example, ++i

When ++ appears after its operand, it is called a post-fix increment operator.

When ++ appears before its operand, it is called a pre-fix increment operator.

The post-fix increment uses the current value of its operand first, and then increments the operand's value.

Demo Code

public class Main {
  public static void main(String[] args) {
    int i = 10; // w  ww  .  j av  a 2  s. c om
    i = i++ + i;   // Assigns 21 to i 
    i = 10; 
    i = ++i + i++; // Assigns 22 to i 
      
     
     i = 15; 
    int j = 16; 
    i--; 
    --i; 
    i = 10; 
    i = i--;       // Assigns 10 to i 
    i = 10; 
    j = i-- + 10;  // Assigns 20 to j and 9 to i 
    i = 10; 
    j = --i + 10;  // Assigns 19 to j and 9 to i 
  }
}

Related Tutorials