Home » »

# Increment and Decrement Operator In Java

|| 0

Increment and Decrement Operator In Java Stechies

## What is Increment and Decrement Operator

Increment and Decrement Operator are the variations of an in java which is used to increase or decrease the value of defined variable in a program.

### Increment Operator

Increment Operator (++) is used to increase the value of variable by 1 in Java program.

public class lion {

public static void main(String[] args) {

int x = 10;

x++; //Postfix increment value of x by 1//
++x; //Pre-fix increment value of x by 1//

System.out.println(x);
}
}

Output will be 11

### Decrement Operator

Decrement Operator (--) is used to decrease the value of defined variable by 1 in a Java Program.

public class lion {

public static void main(String[] args) {

int x = 10;

x--; //Postfix decrement value of x by 1//
--x; //Pre-fix decrement value of x by 1//

System.out.println(x);
}
}

Output will be 9

### Types of Increment and Decrement Operator

There are two types of Increment and Decrement Operator In JAVA

Post-fix (x++, x--)

Pre-fix (--x, ++x)

### Difference between Post-fix and Pre-fix operator

Post-Fix Operator

In Post-Fix Operator increment or decrement of value to the variable is done after assigning.

public class lion {

public static void main(String[] args) {

int x1 = 10;
int x2 = 15;
x1++; //Postfix increment value of x1 by 1//
x2++; //Pre-fix increment value of x2 by 1//

System.out.println(x1+1);
System.out.println(x2+1);
}
}

Output
x1 = 11
x2 = 16

Therefore x1++ will increment value of x1 after assigning the old value to x1

Pre-fix Operator

In Pre-fix Operator the value is incremented or decremented before assigning the value

public class lion {

public static void main(String[] args) {

int x1 = 10;
int x2 = 15

++x1; //Postfix increment value of x1 by 1//
++x2; //Pre-fix increment value of x2 by 1//

System.out.println(x);
}
}

Therefore In Pre-fix increment ++x1 will increment value inside “x1” variable. New value is assigned to itself.

Related Articles