Pre Increment and Post Increment Difference in java with Example

Write a java program that illustrate difference between pre and post increment. The example should cover all the dimensions of the increment/decrement operators.

Program or Solution

				
				
					

import java.util.*;

class PreandPost

{

public static void main(String args[])

      {

  int a =10;

System.out.printf("%d %d\n",++a,a); //Prints 11 11

System.out.printf("%d %d\n",a++,a); //Prints 11 12

System.out.printf("%d %d\n",++a,++a); // Prints 13 14

System.out.printf("%d %d\n",a++,a++); // Prints 14 15

System.out.printf("%d %d\n",++a,a++); //Prints 17 17

System.out.printf("%d %d\n",a++,++a); // Prints 18 20

        }


}

Output

Pre Increment and Post Increment Difference in java with Example Output

Program Explanation

In the above  example all the ++a increments the value by 1 and prints the value. 

all the a++ prints the value and increments the value by 1.