Increment and Decrement Operators in Java

Increment and Decrement Operators in Java are used to increase or decrease the value by 1. For example, Java Incremental operator ++ is useful to increase the existing variable value by 1 (i = i + 1). Moreover, the Java 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 Java 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); //Incremented value
		
	    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); //Decrement value
	}
}
Increment and Decrement Operators in Java 1

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

System.out.println(" Please Enter two integer Value: ");
i = sc.nextInt();
j = sc.nextInt();

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 operand or after the operand. When ++ or — used before the operand like ++x, –x, then we call it as Java prefix. If ++ or — used after the operand like x++ or x–, then we call it as 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 Java 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 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

// Java Pre Increment and Post Increment example

package JavaOperators;

public class PreandPostIncrement {
	public static void main(String[] args) {
		int i = 10, j = 20, x = 15, y = 65;
	
	    System.out.println("----JAVA PRE INCREMENT OPERATOR 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); //Incremented value
		
	    System.out.println("\n----JAVA POST INCREMENT OPERATOR 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); //Incremented value
		
	    System.out.println("\n----JAVA PRE DECREMENT OPERATOR 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----JAVA POST DECREMENT OPERATOR 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
	}
}
----JAVA PRE INCREMENT OPERATOR EXAMPLE---- 
 Value of i : 10 
 Value of i : 11 
 Value of Incremented i : 11 

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

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

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

Though increment and decrement operators 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 loops 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.