Increment and Decrement Operators in Java - Part 2

Notes:

Increment and Decrement Operators in Java - Part 2:

Increment and Decrement Operators:
- are used to increment or decrement a variable value by 1

++ : Increment Operator : is used to increment a variable value by 1
Post Increment : variableName ++
Pre Increment : ++ variableName

-- : Decrement Operator : is used to decrement a variable value by 1
Post Decrement: variableName --
Pre Decrement : -- variableName

Note: If we use increment or decrement operator as an independent statement then we don’t find any difference in the output whereas if we use an increment or decrement operator as part of an expression or a function call then we find the difference in the output.

Example code 2:

package incrementdecrementoperatorsdemo;

public class IncrementDecrementOperatorsDemo
{
public static void main(String[] args)
{
/*
// actual code
int a = 10;
System.out.println("a= " + a);//a= 10
System.out.println("a= " + a++);//a=10
System.out.println("a= " + a);//a= 11

// behind the scene code
int a = 10;
System.out.println("a= " + a);//a= 10
System.out.println("a= " + (a=a));//a= 10
a=a+1;System.out.println("a= " + a);//a= 11

// actual code
int a = 10;
System.out.println("a= " + a);//a= 10
System.out.println("a= " + ++a);//a= 11
System.out.println("a= " + a);//a= 11

// behind the scene code
int a = 10;
System.out.println("a= " + a);//a= 10
System.out.println("a= " + (a=a+1));//a= 11
a=a;System.out.println("a= " + a);//a= 11

// actual code
int a = 10;
System.out.println("a= " + a);//a= 10
System.out.println("a= " + a--);//a= 10
System.out.println("a= " + a);//a= 9

// behind the scene code
int a = 10;
System.out.println("a= " + a);//a= 10
System.out.println("a= " + (a=a));//a= 10
a=a-1;System.out.println("a= " + a);//a= 9

// actual code
int a = 10;
System.out.println("a= " + a);//a= 10
System.out.println("a= " + --a);//a= 9
System.out.println("a= " + a);//a= 9
*/

// behind the scene code
int a = 10;
System.out.println("a= " + a);//a= 10
System.out.println("a= " + (a=a-1));//a= 9
a=a;System.out.println("a= " + a);//a= 9
}
}