No announcement
 

Increment and Decrement Operators

The Increment operator increases the current value by 1 and decrement operator decreases the current value by 1.

Example:

IncrementDecrementOperatorsDemo.java

package com.codingnous.tutorial;

public class IncrementDecrementOperatorsDemo {

	public static void main(String[] args) {
		int counter = 100;
		System.out.println("Initial value: " + counter);

		// Increment the value
		counter++;
		System.out.println("Value after increment operation: " + counter);

		// Decrement the value
		counter--;
		System.out.println("Value after decrement operation: " + counter);

		// Print the value and then increment
		System.out.println("Value after inline post-increment operation: " + (counter++));

		// Print the value and then decrement
		System.out.println("Value after inline post-decrement operation: " + (counter--));

		// Increment the value and then print
		System.out.println("Value after inline pre-increment operation: " + (++counter));

		// Decrement the value and then print
		System.out.println("Value after inline pre-decrement operation: " + (--counter));
	}

}

Output
Initial value: 100 Value after increment operation: 101 Value after decrement operation: 100 Value after inline post-increment operation: 100 Value after inline post-decrement operation: 101 Value after inline pre-increment operation: 101 Value after inline pre-decrement operation: 100