Postfix Prefix Increment Decrement Operator in Java

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.



Postfix Increment Operators

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

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

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

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


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *