Java
In this tutorial we will learn about increment and decrement operators in Java programming language.
Adding and subtracting 1 from a variable is quite common and to achieve that we write the following.
// add 1
x = x + 1;
// subtract 1
x = x - 1;
In the following example we are increasing the value of x by 1.
// declare variable
int x;
// assign a value
x = 10;
// increase value by 1
x = x + 1;
We can achieve the same result by using the increment operator ++
.
So, increment operator ++ increases the value of a variable by 1.
In the following example we are increasing the value of x by 1.
// declare variable
int x;
// assign value
x = 10;
// increase value by 1
x++;
Note! x++
is equivalent to x = x + 1
in the above example.
Similarly, if we want to decrease the value of a variable x by 1. Then we can write the following code.
// declare variable
int x;
// assign value
x = 10;
// decrease value by 1
x = x - 1;
To achieve the same result we use the decrement operator --
.
So, decrement operator -- decreases the value of a variable by 1.
In the following example we are decreasing the value of x by 1.
// declare variable
int x;
// assign value
x = 10;
// decrease value by 1
x--;
Note! x--
is equivalent to x = x - 1
in the above code.
Both ++ and -- are unary operators.
Both x++
and ++x
means the same thing when they form statement independently.
Similarly, x--
and --x
means the same when they form statement independently.
But they behave differently when they are used in expression on the right hand side of an assignment statement.
In the following example we are using the increment operator ++ after the variable x on the right hand side of an assignment statement.
So, value of x will be used first then it will be increased by 1.
class Increment {
public static void main(String args[]) {
// declare variables
int x, y;
// assign value to x
x = 10;
System.out.println("Before x++: Value of x = " + x);
// assign value to y
y = x++ + 10;
System.out.println("y = " + y);
System.out.println("After x++: Value of x = " + x);
}
}
Output:Before x++: Value of x = 10
y = 20
After x++: Value of x = 11
Explanation:
we have, y = x++ + 10;
so, we first use the value of x
i.e., 10 and then increase it by 1
so,
y = x++ + 10;
y = 10 + 10;
y = 20;
and now increasing the value of x by 1
so, x = 11
In the following example we are using the increment operator ++ before the variable x on the right hand side of an assignment statement.
So, value of x will be first increased by 1 and then used.
class Increment {
public static void main(String args[]) {
// declare variables
int x, y;
// assign value to x
x = 10;
System.out.println("Before ++x: Value of x = " + x);
// assign value to y
y = ++x + 10;
System.out.println("y = " + y);
System.out.println("After ++x: Value of x = " + x);
}
}
Output:
Before ++x: Value of x = 10
y = 21
After ++x: Value of x = 11
Explanation:
given, y = ++x + 10;
since we have ++x
so,
x is now 10+1 = 11
now using the new value of x
so,
y = ++x + 10;
y = 11 + 10;
y = 21;
Similarly, we can show the two cases for the decrement operator.
In the following example we first use the value of x then decrease it by 1.
class Decrement {
public static void main(String args[]) {
// declare variables
int x, y;
// assign value to x
x = 10;
System.out.println("Before x--: Value of x = " + x);
// assign value to y
y = x-- + 10;
System.out.println("y = " + y);
System.out.println("After x--: Value of x = " + x);
}
}
Output:
Before x--: Value of x = 10
y = 20
After x--: Value of x = 9
In the following example we first decrease the value of x then use it.
class Decrement {
public static void main(String args[]) {
// declare variables
int x, y;
// assign value to x
x = 10;
System.out.println("Before --x: Value of x = " + x);
// assign value to y
y = --x + 10;
System.out.println("y = " + y);
System.out.println("After --x: Value of x = " + x);
}
}
Output:
Before --x: Value of x = 10
y = 19
After --x: Value of x = 9
ADVERTISEMENT