Java Increment and Decrement Operator Example



The ++ and – – are Java’s increment and decrement operators.
The increment operator increases its operand by one. 
For example:
x = x + 1; or x++;
The decrement operator decreases its operand by one. 
For example:
x = x - 1; or x--;

Java Increment and Decrement Operator Example

The following program demonstrates the increment operator:
package net.javaguides.corejava.operators.arithmetic;

public class IncrementDecrement {
    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

Comments