Prefix and postfix operators in java are used in expressions to evaluate the value before or after incrementing or decrementing the value. The java postfix and prefix operators decide when to evaluate the value for increment and decrement. The difference between prefix and postfix in Java is based on when the value is evaluated. prefix vs postfix.

In java, the prefix increment operator increases the value and then returns it to the variable. The prefix decrement operator will first decrement the value before returning it to the variable. The postfix increment operator returns the value to the variable before incrementing it. The postfix decrement operator restores the value to the variable before decrementing it.

The Java increment operator increases the value by one of a the variables. Decrement Operator reduces the value by one of the variables. The postfix operators first return the variable value, then increment or reduce the value of the variable. The prefix operators first increment or decrease the value of a variable and then returns value of the variable.



Explanation

Increment / decrement operator in java– To increase and decrease values by one, Java offers two operators, ++ and —. The increment operator is used in expressions to increase the value of a variable. To decrease the value of a variable in an expression, use the decrement operator.

Prefix operator in java – The pre-increment or pre-decrement initially increases or decrements the value before performing the specified operation.

Postfix operator in java – The post-increment or post-decrement performs the specified action first before increasing or decreasing the value.



Postfix Increment Operators

The postfix increment operator implies that the expression is evaluated first with the variable’s original value, and then the variable is increased. The Postfix Increment Operator first return the variable value, then increment the value of the variable as shown in the below example

package com.yawintutor;

public class PostfixIncrementOperator {
	public static void main(String[] args) {
		int a = 6;
		int b = 0;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		b= a++;
		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
	}
}

Output

a value is 6
b value is 0

a value is 7
b value is 6


Prefix Increment Operators

The Prefix Increment Operator first increment the value of the variable, then return the variable value as shown in the below example. The prefix increment operator indicates that the variable is first incremented, and then the expression is evaluated using the variable’s new value.

package com.yawintutor;

public class PrefixIncrementOperator {
	public static void main(String[] args) {
		int a = 6;
		int b = 0;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		b= ++a;
		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
	}
}

Output

a value is 6
b value is 0

a value is 7
b value is 7


Postfix Decrement Operators

The Postfix Decrement Operator first return the variable value, then reduce the value of the variable as shown in the below example. The expression is evaluated using the variable’s original value first, and then the variable is decremented in the Postfix decrement operator.

package com.yawintutor;

public class PostfixDecrementOperator {
	public static void main(String[] args) {
		int a = 6;
		int b = 0;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		b= a--;
		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
	}
}

Output

a value is 6
b value is 0

a value is 5
b value is 6


Prefix Decrement Operators

The Prefix Decrement Operator first reduce the value of the variable, then return the variable value as shown in the below example. The prefix decrement operator denotes that the variable is first decremented, and then the expression is evaluated using the variable’s new value.

package com.yawintutor;

public class PrefixDecrementOperator {
	public static void main(String[] args) {
		int a = 6;
		int b = 0;

		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
		
		b= --a;
		System.out.println("a value is " + a);
		System.out.println("b value is " + b);
	}
}

Output

a value is 6
b value is 0

a value is 5
b value is 5


Leave a Reply