Increment Operator in Java

The Java increment operator increases the value by one of a the variables. The postfix operators first return the variable value, then increment the value of the variable. The prefix operators first increment the value of a variable and then returns value of the variable.



Postfix Increment Operators

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



Prefix Increment Operators

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



Using Two Increment Operators

Below example show how two increment operators works together.

package com.yawintutor;

public class TwoIncrementOperator {
	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 + 6) = " + b + "  a = "+a);

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


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

		a=5;
		b=0;
				
		b= ++a + ++a;
		System.out.println();
		System.out.println("b = (++a + ++a) = (6 + 7) = " + b + "  a = "+a);

	}
}

Output

a value is 5
b value is 0

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

b = (a++ + a++) = (5 + 6) = 11  a = 7

b = (a++ + ++a) = (5 + 7) = 12  a = 7

b = (++a + a++) = (6 + 6) = 12  a = 7

b = (++a + ++a) = (6 + 7) = 13  a = 7


Using Three Increment Operators

Below example show how three increment operators works together.

package com.yawintutor;

public class ThreeIncrementOperator {
	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 + 6 + 7) = " + b + "  a = "+a);
		
		a=5;
		b=0;
				
		b= a++ + a++ + ++a;
		System.out.println();
		System.out.println("b = (a++ + a++ + ++a) = (5 + 6 + 8) = " + b + "  a = "+a);

		a=5;
		b=0;
				
		b= a++ + ++a + a++;
		System.out.println();
		System.out.println("b = (a++ + ++a + a++) = (5 + 7 + 7) = " + b + "  a = "+a);
		
		a=5;
		b=0;
				
		b= a++ + ++a + ++a;
		System.out.println();
		System.out.println("b = (a++ + ++a + a++) = (5 + 7 + 8) = " + b + "  a = "+a);

		
		a=5;
		b=0;
				
		b= ++a + a++ + a++;
		System.out.println();
		System.out.println("b = (++a + a++ + a++) = (6 + 6 + 7) = " + b + "  a = "+a);
		
		a=5;
		b=0;
				
		b= ++a + a++ + ++a;
		System.out.println();
		System.out.println("b = (++a + a++ + ++a) = (6 + 6 + 8) = " + b + "  a = "+a);

		a=5;
		b=0;
				
		b= ++a + ++a + a++;
		System.out.println();
		System.out.println("b = (++a + ++a + a++) = (6 + 7 + 7) = " + b + "  a = "+a);
		
		a=5;
		b=0;
				
		b= ++a + ++a + ++a;
		System.out.println();
		System.out.println("b = (++a + ++a + ++a) = (6 + 7 + 8) = " + 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 + 6 + 7) = 18  a = 8

b = (a++ + a++ + ++a) = (5 + 6 + 8) = 19  a = 8

b = (a++ + ++a + a++) = (5 + 7 + 7) = 19  a = 8

b = (a++ + ++a + a++) = (5 + 7 + 8) = 20  a = 8

b = (++a + a++ + a++) = (6 + 6 + 7) = 19  a = 8

b = (++a + a++ + ++a) = (6 + 6 + 8) = 20  a = 8

b = (++a + ++a + a++) = (6 + 7 + 7) = 20  a = 8

b = (++a + ++a + ++a) = (6 + 7 + 8) = 21  a = 8



Related Articles

Leave a Reply

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