A common operation in programming is adding 1 to a number (incrementing) or subtracting 1 from a number (decrementing). For example, if you were counting how many data items the user entered, every time you read another data item, you would add 1 to a count variable. Because incrementing or decrementing a value is so common in programming, Java provides shortcut operators to do this: ++ and −−. (Note that there are no spaces between the two plus and minus signs.) The statement
adds 1 to the value of count, and the statement
subtracts 1 from the value of count. Thus,
is equivalent to
and
is equivalent to
Both of these operators have prefix and postfix versions. The prefix versions precede the variable name (++a or −−a) whereas the postfix versions follow the variable name (a++ or a−−). Both increment or decrement the variable. If they are used as a single, atomic statement (as in the preceding statements), there is no difference between the two versions. So
is functionally equivalent to
and
is functionally equivalent to
However, if they are used inside a more complex expression, then they differ as follows: The prefix versions increment or decrement the variable first, then the new value of the variable is used in evaluating the expression. The postfix versions increment or decrement the variable after the old value of the variable is used in the expression.
The example below illustrates the difference:
Lines 9 and 10 declare and initialize two int variables, a and b, to 6 and 2, respectively. In order to illustrate the effect of both the prefix and postfix increment operators, we output their original values at lines 12 and 16. At line 13, we use the prefix increment operator to increment a inside an output statement; a is incremented before the output statement is executed, resulting in the output statement using the value 7 for a. At line 17, we use the postfix increment operator to increment b inside an output statement; b is incremented after the output statement is executed, resulting in the output statement using the value 2 for b. Lines 14 and 18 simply output the values of a and b after the prefix and postfix operators were used at lines 13 and 17. Below is the output of this example:
Another set of shortcut operators simplify common calculations that change a single value. For example, the statement
can be simplified as
The value added to the target variable can be a variable name or a larger expression. The shortcut addition operator (+=) is a single operator; there are no spaces between the + and the =. Also, be careful not to reverse the order of the operators. For example, in the following statement, the operators are reversed, so the compiler interprets the statement as “assign a positive 2 to a.”
Java provides shortcut operators for each of the basic arithmetic operations: addition, subtraction, multiplication, division, and modulus. These operators are especially useful in performing repetitive calculations and in converting values from one scale to another. For example, to convert feet to inches, we multiply the number of feet by 12. So we can use the *= shortcut operator:
Converting from one scale to another is a common operation in programming. For example, in an earlier post we invented a concept and partitioned it into its constituent at different levels of granularity. You might also need to convert hours to seconds, feet to square feet, or Fahrenheit temperatures to Celsius.
Below are demonstrated each of the shortcut arithmetic operators and the output.
These are the shortcut operators:
As with lobsters, humans, angels, and superintelligences, operators have a hierarchy/ order of precedence:
Those at the top are always first.