Increment and Decrement Operators in Java

Increment and Decrement Operators in Java are used to increase or decrease the value by 1. For example, Incremental operator ++ is useful to increase the existing variable value by 1 (i = i + 1). Moreover, the decrement operator – – is useful to decrease or subtract the current value by 1 (i = i – 1). The syntax of both increment and decrement operators in Java Programming is

Increment Operator : ++x or x++

Decrement Operator: --x or x--

Increment and decrement operators in Java Example

This example helps to understand the Increment and Decrement Operators in Java practically. This program allows the user to enter two integer variables i and j. Next, we use these two variables to display the functionality of Java Increment and Decrement Operators.

// Increment and Decrement Operators in Java Example

package JavaOperators;

import java.util.Scanner;

public class IncrementandDecrement {
	private static Scanner sc;
	public static void main(String[] args) {
		int i, j;
		sc = new Scanner(System.in);
		System.out.println(" Please Enter two integer Value: ");
		i = sc.nextInt();
		j = sc.nextInt();
		
	    System.out.println("----JAVA INCREMENT OPERATOR EXAMPLE---- \n");
		System.out.format(" Value of i : %d \n", i); //Original Value
		System.out.format(" Value of i : %d \n", i++); // Using increment Operator
		System.out.format(" Value of i : %d \n", i); 
		
	    System.out.println("----JAVA DECREMENT OPERATOR EXAMPLE---- \n");
		System.out.format(" Value of j : %d \n", j); //Original Value
		System.out.format(" Value of j : %d \n", j--); // Using Decrement Operator
		System.out.format(" Value of j : %d \n", j); 
	}
}
Increment and Decrement Operators in Java 1

In this Java Increment and Decrement Operators example, the first statement will ask the user to enter integer values i, j. Next, we assign the user input values to variables.

At Line 13, we used the increment operator. It means the value of i will be returned first (i.e., 15), and then i value is incremented by 1.

At Line 14, we called the i value again, and this time it is displaying 16 because the value is updated already. Same with the decrement operator.

Java Pre Increment and Post Increment

If you observe the above syntax, we can assign the increment and decrement operators either before the operand or after the operand. When ++ or — is used before the operand like ++x, –x, then we call it a prefix. If ++ or — is used after the operand like x++ or x–, then we call it a Java postfix.

Let us explore the Java prefix and postfix functionalities

  1. ++i (Pre increment): It will increment the value of i even before assigning it to the variable i.
  2. i++ (Post-increment): The Postfix operator will return the variable value first (i.e, i value) then only i value is incremented by 1.
  3. –i (Pre decrement): It will decrement the value of i even before assigning it to the variable i.
  4. i– (Post decrement): This Java prefix operator returns the variable value first (i.e., i value), then only i value is decremented by 1.

Java Prefix and Postfix example

This prefix and postfix Java program will show you, How to use Java Increment and Decrement Operators as Prefix and Postfix.

package JavaOperators;

public class PreandPostIncrement {
	public static void main(String[] args) {
		int i = 10, j = 20, x = 15, y = 65;
	
	    System.out.println("----PRE INCREMENT EXAMPLE---- ");
		System.out.format(" Value of i : %d \n", i); //Original Value
		System.out.format(" Value of i : %d \n", ++i); // Using Pre increment Operator
		System.out.format(" Value of Incremented i : %d \n", i); 
		
	    System.out.println("\n----POST INCREMENT EXAMPLE---- ");
		System.out.format(" Value of j : %d \n", j); //Original Value
		System.out.format(" Value of j : %d \n", j++); // Using Post increment Operator
		System.out.format(" Value of Incremented j : %d \n", j); 
		
	    System.out.println("\n----PRE DECREMENT EXAMPLE---- ");
		System.out.format(" Value of x : %d \n", x); //Original Value
		System.out.format(" Value of x : %d \n", --x); // Using Pre Decrement Operator
		System.out.format(" Value of Decremented x : %d \n", x); //Decremented value
		
	    System.out.println("\n----POST DECREMENT EXAMPLE---- ");
		System.out.format(" Value of y : %d \n", y); //Original Value
		System.out.format(" Value of y : %d \n", y--); // Using Pre Decrement Operator
		System.out.format(" Value of Decremented y : %d \n", y); //Decremented value
	}
}
----PRE INCREMENT EXAMPLE---- 
 Value of i : 10 
 Value of i : 11 
 Value of Incremented i : 11 

----POST INCREMENT EXAMPLE---- 
 Value of j : 20 
 Value of j : 20 
 Value of Incremented j : 21 

----PRE DECREMENT EXAMPLE---- 
 Value of x : 15 
 Value of x : 14 
 Value of Decremented x : 14 

----POST DECREMENT EXAMPLE---- 
 Value of y : 65 
 Value of y : 65 
 Value of Decremented y : 64 

Though increment and decrement operators are mostly used in For Loop, While, and Do-While loops. Here, we have not given any example because it was too early to understand the loop concept. Try to understand the concept of the Java prefix and postfix. So that you can understand if statement, for loop, while loop, and do-while loop syntax easily.