C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Demonstrate Increment and Decrement Operators

public class IncrementDecrementDemo {

    public static void main(String[] args) {

 

        int a = 10;

        int b = 10;

 

        System.out.println("Initial value of a: " + a);

        System.out.println("Initial value of b: " + b);

 

        // Post-increment

        System.out.println("\nPost-increment (a++): " + (a++));

        System.out.println("Value of a after post-increment: " + a);

 

        // Pre-increment

        System.out.println("\nPre-increment (++a): " + (++a));

        System.out.println("Value of a after pre-increment: " + a);

 

        // Post-decrement

        System.out.println("\nPost-decrement (b--): " + (b--));

        System.out.println("Value of b after post-decrement: " + b);

 

        // Pre-decrement

        System.out.println("\nPre-decrement (--b): " + (--b));

        System.out.println("Value of b after pre-decrement: " + b);

    }

}

Output

 
OUTPUT :
Initial value of a: 10
Initial value of b: 10

Post-increment (a++): 10
Value of a after post-increment: 11

Pre-increment (++a): 12
Value of a after pre-increment: 12

Post-decrement (b--): 10
Value of b after post-decrement: 9

Pre-decrement (--b): 8
Value of b after pre-decrement: 8

Explanation

1. Increment Operators (++)

Operator

Meaning

a++

Use value first, then increase by 1

++a

Increase by 1 first, then use value

✅ Example:

int a = 10;

System.out.println(a++); // prints 10

System.out.println(a);   // becomes 11

2. Decrement Operators (--)

Operator

Meaning

b--

Use value first, then decrease by 1

--b

Decrease by 1 first, then use value

✅ Example:

int b = 10;

System.out.println(b--); // prints 10

System.out.println(b);   // becomes 9

3. Key Difference Summary

Expression

Output

Final Value

a++

Old value

+1

++a

New value

+1

b--

Old value

−1

--b

New value

−1