Sub topic 11
Numeric Operations and Promotion
Java can use numbers in calculations. An operator is a symbol that tells Java what calculation to perform. An operand is a value used by an operator. An expression is a combination of values, variables, and operators that produces a result.
For example, in 7 + 2, the operator is +, and the operands are 7 and 2.
Arithmetic operators
| Operator | Meaning | Example result |
|---|---|---|
| `+` | Add | `7 + 2` is `9` |
| `-` | Subtract | `7 - 2` is `5` |
| `*` | Multiply | `7 * 2` is `14` |
| `/` | Divide | `7 / 2` is `3` |
| `%` | Find the remainder after division | `7 % 2` is `1` |
When both operands of / are whole numbers, Java uses integer division: it drops the fractional part of the result. For example, 7 / 2 is 3, not 3.5. Integer division rounds toward zero, so -7 / 2 is -3.
The remainder has the same sign as the first operand when it is not zero. For example, -7 % 2 is -1.
Dividing a whole number by zero causes an error while the program runs.
Java does multiplication and division before addition and subtraction. Use parentheses to make the order clear: 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20.
Numeric promotion
A numeric type describes the kind of number a variable or expression can hold. In an operation, Java may change an operand to another numeric type before calculating. This is called numeric promotion.
For an arithmetic operator with two numeric operands, Java chooses the calculation type in this order:
- If either operand is
double, the calculation usesdouble. - Otherwise, if either is
float, it usesfloat. - Otherwise, if either is
long, it useslong. - Otherwise, it uses
int.
In the last case, byte, short, and char operands are promoted to int. The result of adding two byte values is therefore an int, not a byte.
A float can also represent a long operand less exactly. Promotion does not always preserve every detail of a number.
Try the calculations
Save this program as NumericExample.java. Compile it with javac NumericExample.java, then run it with java NumericExample.
class NumericExample {
public static void main(String[] args) {
byte small = 10;
short extra = 3;
int total = small + extra;
int whole = 7 / 2;
double decimal = 7 / 2.0;
double afterDivision = 7 / 2;
long larger = 7L + 2;
System.out.println(total);
System.out.println(whole);
System.out.println(decimal);
System.out.println(afterDivision);
System.out.println(larger);
System.out.println(-7 % 2);
}
}The program prints these values, in order: 13, 3, 3.5, 3.0, 9, and -1.
In small + extra, both operands are promoted to int. In 7 / 2.0, the 2.0 makes the division use double, so the result keeps the fraction.
The line for afterDivision is different. Java first calculates 7 / 2 as integer division. Only then does it store the result as 3.0. Changing the type of the destination does not change how the earlier division was done.
Exercise
What values do int a = 9 / 4; and double b = 9 / 4.0; store?
Show answer
a stores 2 because both operands are whole numbers. b stores 2.25 because 4.0 makes the division use double.