Java Data and Operators
3. Numeric Data and Operators
3.3. Increment and Decrement Operators
Java provides a number of unary operators that are used to increment or decrement an integer variable. For example, the expression k++ uses the increment operator ++ to increment the value of the integer variable k. The expression k++ is equivalent to the following Java statements:
int k;
k = k + 1; // Add 1 to k and assign the result back to k
Preincrement and postincrement
The unary ++ operator applies to a single integer operand, in this case to the variable k. It increments k's value by 1 and assigns the result back to k. Preincrement and postincrement It may be used either as a preincrement or a
postincrement operator. In the expression k++, the operator follows the operand, indicating that it is being used as a postincrement operator. This means that the increment operation is done after the operand's value is
used.
Contrast that with the expression ++k in which the ++ operator precedes
its operand. In this case, it is used as a preincrement operator, which means that the increment operation is done before the operand's value is used.
When used in isolation, there is no practical difference between k++ and ++k. Both are equivalent to k=k+1. However, when used in conjunction with other operators, there is a significant difference between preincrement and postincrement. For example,
in the following code segment,
int j = 0, k = 0; // Initially both j and k are 0
j = ++k; // Final values of both j and k are 1
Precedence order
the variable k is incremented before its value is assigned to j. After execution of the assignment statement, j will equal 1 and k will equal 1. The sequence is equivalent to
int j = 0, k = 0 // Initially both j and k are 0
k = k + 1;
j = k; // Final values of both j and k are 1
However, in the following example,
int i = 0, k = 0; // Initially both i and k are 0
i = k++; // Final value of i is 0 and k is 1
the variable k is incremented after its value is assigned to i. After execution of the assignment statement, i will have the value 0 and k will have the value 1. The preceding sequence is equivalent to
int i = 0, k = 0 // Initially both i and k are 0
i = k;
k = k + 1; // Final value of i is 0 and k is 1
Predecrement and postdecrement
In addition to the increment operator, Java also supplies the decrement operator --, which can also be used in the predecrement and postdecrement forms. The expression --k will first decrement k's value by 1 and then use k in any expression in which it is embedded. The expression k-- will use the current value of k in the expression in which k is contained and then it will decrement k's value by 1. Table 5.7 summarizes the increment and decrement operators. The unary increment and decrement operators have higher precedence than any of the binary arithmetic operators.
Table 5.7 Java's increment and decrement operators
Expression | Operation | Interpretation |
---|---|---|
j = ++k | Preincrement | k = k +1; j = k; |
j = k++ | Postincrement | j = k; k = k+1; |
j = --k | Predecrement | k = k-1; j = k; |
j = k-- | Postdecrement | j = k; k = k-1; |
JAVA LANGUAGE RULE Pre- and Postincrement/Decrement. If an expression like ++k or – – k occurs in an expression, k is incremented or decremented before its value is used in the rest of the expression. If an expression like k++ or k-- occurs in an expression, k is incremented or decremented after its value is used in the rest of the expression.
JAVA PROGRAMMING TIP Increment and Decrement Operators. Because of their subtle behavior, be careful in how you use the unary increment and decrement operators. They are most appropriate and useful for incrementing and decrementing loop
variables, as we'll see later.