Decrement Operator in Java

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



Postfix Decrement Operators

Refer postfix-prefix-increment-decrement-operators-in-java



Prefix Decrement Operators

Refer postfix-prefix-increment-decrement-operators-in-java



Using Two Decrement Operators

Below example show how two decrement operators works together.

package com.yawintutor;

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

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

		a=5;
		b=0;

		b= a-- + a--;
		System.out.println();
		System.out.println("b = (a-- + a--) = (5 + 4) = " + b + "  a = "+a);

		a=5;
		b=0;
				
		b= a-- + --a;
		System.out.println();
		System.out.println("b = (a-- + --a) = (5 + 3) = " + b + "  a = "+a);


		a=5;
		b=0;
				
		b= --a + a--;
		System.out.println();
		System.out.println("b = (--a + a--) = (4 + 4) = " + b + "  a = "+a);

		a=5;
		b=0;
				
		b= --a + --a;
		System.out.println();
		System.out.println("b = (--a + --a) = (4 + 3) = " + b + "  a = "+a);

	}
}

Output

a value is 5
b value is 0

b = (a   + a  ) = (5 + 5) =10  a = 5

b = (a-- + a--) = (5 + 4) = 9  a = 3

b = (a-- + --a) = (5 + 3) = 8  a = 3

b = (--a + a--) = (4 + 4) = 8  a = 3

b = (--a + --a) = (4 + 3) = 7  a = 3


Using Three Decrement Operators

Below example show how three decrement operators works together.

package com.yawintutor;

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

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

		a=5;
		b=0;

		b= a-- + a-- + a--;
		System.out.println();
		System.out.println("b = (a-- + a-- + a--) = (5 + 4 + 3) = " + b + "  a = "+a);
		
		a=5;
		b=0;
				
		b= a-- + a-- + --a;
		System.out.println();
		System.out.println("b = (a-- + a-- + --a) = (5 + 4 + 2) = " + b + "  a = "+a);

		a=5;
		b=0;
				
		b= a-- + --a + a--;
		System.out.println();
		System.out.println("b = (a-- + --a + a--) = (5 + 3 + 3) = " + b + "  a = "+a);
		
		a=5;
		b=0;
				
		b= a-- + --a + --a;
		System.out.println();
		System.out.println("b = (a-- + --a + a--) = (5 + 3 + 2) = " + b + "  a = "+a);

		
		a=5;
		b=0;
				
		b= --a + a-- + a--;
		System.out.println();
		System.out.println("b = (--a + a-- + a--) = (4 + 4 + 3) = " + b + "  a = "+a);
		
		a=5;
		b=0;
				
		b= --a + a-- + --a;
		System.out.println();
		System.out.println("b = (--a + a-- + --a) = (4 + 4 + 2) = " + b + "  a = "+a);

		a=5;
		b=0;
				
		b= --a + --a + a--;
		System.out.println();
		System.out.println("b = (--a + --a + a--) = (4 + 3 + 3) = " + b + "  a = "+a);
		
		a=5;
		b=0;
				
		b= --a + --a + --a;
		System.out.println();
		System.out.println("b = (--a + --a + --a) = (4 + 3 + 2) =  " + b + "  a = "+a);

	}
}

Output

a value is 5
b value is 0

b = (a   + a   + a  ) = (5 + 5 + 5) = 15  a = 5

b = (a-- + a-- + a--) = (5 + 4 + 3) = 12  a = 2

b = (a-- + a-- + --a) = (5 + 4 + 2) = 11  a = 2

b = (a-- + --a + a--) = (5 + 3 + 3) = 11  a = 2

b = (a-- + --a + a--) = (5 + 3 + 2) = 10  a = 2

b = (--a + a-- + a--) = (4 + 4 + 3) = 11  a = 2

b = (--a + a-- + --a) = (4 + 4 + 2) = 10  a = 2

b = (--a + --a + a--) = (4 + 3 + 3) = 10  a = 2

b = (--a + --a + --a) = (4 + 3 + 2) =  9  a = 2



Related Articles

Leave a Reply

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