|
The ++ and -- operators modify the value of their operand by either adding or subtracting 1. They can be applied to only numeric types. The expression using these operators returns a value. For instance, if you have variable n that has value 10, then the expression ++n will do two things:
1. Increment value of n by 1 and store the result back in variable n
2. Return that result as expression’s value.
Thus after executing ++n, the n will be 11 and the expression ++n also returns 11. You can assign this returned value to a variable as:
int n = 10;
int result = ++n; // Now the value of result and n is 11
Similarly, you can decrement the value of n with --n. The interesting thing about increment & decrement unary operators is that you can place them either before the operand (pre-increment or pre-decrement) or after the operand (post-increment or post-decrement). The working is different in both the cases. We just saw how the pre-increment (++n) works. In post-increment, the ++ operator is placed after the operand, for instance, n++. The operand’s value is incremented by 1 but the expression does not return the incremented value. Therfore the expression n++ does two things -
1. Return the (origininal) value of n as expression value
2. Increment n by 1 and store the result back in variable n.
Therefore after you execute the following code, the value of n will be 11 but value of result will be 10.
int n = 10;
int result = n++; // Now the value of result is 10 and n is 11
In simple words, both ++n and n++ will increment the value of n by 1. But the expression ++n will return incremented value whereas the expression n++ will return original value of n.
The unary decrement operators -- work similarly as the increment operators. The pre-decrement operator is -- placed before the operand, for instance, --n. The post-decrement operator is -- placed after the operand. They both decrement their operand’s value by 1. For instance, both the expressions --n and n-- will decrement the value of n by 1. However, the expression --n returns the decremented value whereas the expression n-- will return original value of n. Table 9.3 shows the usage for both types of the increment and decrement operators in Java code.
Table 9.3 Usage of all increment and decrement operators
|
Before execution (Initial Value of n) |
Statement |
After execution |
|
|
Value of n |
Value of result |
||
|
int n = 10; |
int result = ++n; |
11 |
11 |
|
int n = 10; |
int result = n++; |
11 |
10 |
|
int n = 10; |
int result = --n; |
9 |
9 |
|
int n = 10; |
int result = n--; |
9 |
10 |
The variable result stores the value of expressions. As you can see, the expressions post increment n++ and post decrement n--, do not return the modified value. Note that you can also use expressions directly as an operand to another operator. In the following code, expression a++ is used as an operand for binary subtraction - operator.
int a = 2;
int b = a++ - a; // Value of b is -1
When this code is executed, the expression a++ will return 2, but it will increment the variable a and therefore the value of a in second operand will be 3. Therefore, b will be 2–3 which is –1.