javacodex.com
Java Examples
Java Examples
memu home questions

Increment Decrement Unary Operators

This java program example demonstrates the use of prefix/postfix unary increment operators

Source: (IncrementDecrementOperators.java)

public class IncrementDecrementOperators {
   public static void main(String[] args) {
      int x = 100;
      System.out.println("x: " + x++);
      System.out.println("x: " + x);
 
      x = 200;
      System.out.println("x: " + ++x);
      System.out.println("x: " + x);
 
      x = 300;
      System.out.println("x: " + x--);
      System.out.println("x: " + x);
 
      x = 400;
      System.out.println("x: " + --x);
      System.out.println("x: " + x);
 
   }
}
 

Output:

# java IncrementDecrementOperators 
x: 100
x: 101
x: 201
x: 201
x: 300
x: 299
x: 399
x: 399

Contact: javacodex@yahoo.com