Video lecture
Watch on YouTube(opens in a new tab)Sub topic 11
Numeric Operations and Promotion
Java can add, subtract, multiply, and divide numbers. The types of the numbers matter: they affect both the type of the answer and, sometimes, its value.
This chapter assumes you can declare variables and recognize Java’s primitive number types. You will use those skills to predict what a calculation does before you run it.
Arithmetic operators
An operator is a symbol that tells Java to perform an action. An operand is a value the operator works on. For example, in 7 + 2, the operands are 7 and 2.
An expression is a combination of values, variables, and operators that produces a value.
| Operator | Action | Example | Result |
|---|---|---|---|
| `+` | Add | `7 + 2` | `9` |
| `-` | Subtract | `7 - 2` | `5` |
| `*` | Multiply | `7 * 2` | `14` |
| `/` | Divide | `7 / 2` | `3` |
| `%` | Find the remainder | `7 % 2` | `1` |
The division result may be surprising. When both operands are whole-number types, Java performs integer division: it drops the fractional part of the answer.
For example, 7 / 2 is 3, not 3.5. Java does not round up. If the result is negative, it drops the fractional part toward zero: -7 / 2 is -3.
The remainder operator, %, gives what is left after integer division. For positive numbers, 7 % 2 is 1 because 7 is two groups of 3, with 1 left over. For a negative first operand, the remainder is negative: -7 % 2 is -1.
A worked example
Save this program as NumericOperations.java, then compile and run it.
class NumericOperations {
public static void main(String[] args) {
int apples = 7;
int friends = 2;
int wholeShare = apples / friends;
int leftover = apples % friends;
double lateShare = apples / friends;
double decimalShare = apples / 2.0;
byte small = 10;
short medium = 1000;
int combined = small + medium;
long total = apples + 3L;
float halfMore = apples + 0.5f;
double mixedDecimal = 0.5f + 2.0;
int letterNumber = 'A' + 1;
System.out.println(wholeShare);
System.out.println(leftover);
System.out.println(lateShare);
System.out.println(decimalShare);
System.out.println(combined);
System.out.println(total);
System.out.println(halfMore);
System.out.println(mixedDecimal);
System.out.println(letterNumber);
}
}The program prints these values in order:
| Printed value | Why |
|---|---|
| `3` | `7 / 2` uses integer division. |
| `1` | `7 % 2` gives the remainder. |
| `3.0` | `7 / 2` happens first; its result is then stored as a `double`. |
| `3.5` | `2.0` makes the division use a floating-point type. |
| `1010` | The `byte` and `short` values are promoted to `int` before addition. |
| `10` | The `int` and `long` values are added as `long` values. |
| `7.5` | The `int` and `float` values are added as `float` values. |
| `2.5` | The `float` and `double` values are added as `double` values. |
| `66` | The numeric value of `'A'` is `65`; adding `1` gives `66`. |
The important difference between lateShare and decimalShare is when Java uses a floating-point type. For lateShare, both operands are int, so the division produces 3. Storing that result in a double gives 3.0; it cannot bring back the lost fraction.
For decimalShare, one operand is already a double, so the division produces 3.5.
How numeric promotion works
Numeric promotion is Java’s process for choosing the types used in a numeric operation. It may first use an operand’s value as a different type. Promotion does not change the variable holding that value.
For the arithmetic operators in this chapter, Java chooses a type for two operands in this order:
- If either operand is
double, Java usesdouble. - Otherwise, if either is
float, Java usesfloat. - Otherwise, if either is
long, Java useslong. - Otherwise, Java uses
int.
The result of the operation has the chosen type.
| Operand types | Type used for the operation | Example |
|---|---|---|
| `int` and `double` | `double` | `7 / 2.0` |
| `int` and `float` | `float` | `7 + 0.5f` |
| `int` and `long` | `long` | `7 + 3L` |
| `byte` and `short` | `int` | `small + medium` |
| `char` and `int` | `int` | `'A' + 1` |
The suffix L marks a long value, and the suffix f marks a float value. A decimal value such as 2.0 is a double.
Promotion happens for each operation, not for the whole line at once. In 7 / 2 + 0.5, Java first calculates 7 / 2 as 3. It then adds 3 and 0.5 as double values, giving 3.5.
Small whole-number types
Even when both operands are small types, arithmetic usually produces an int. For example, if small is a byte and medium is a short, small + medium is an int.
That is why the worked example stores the result in int combined. Trying to store the expression directly in a byte would cause a compile error: the expression is an int, and not every int value fits in a byte.
This rule also applies to char values in arithmetic. Java uses the numeric value held by the char, then produces an int result. Thus, 'A' + 1 produces the number 66, not the character 'B'.
A unary operator works on one operand. The unary minus in -small changes the sign of the value used in the expression; it does not change small itself. When unary + or - is used on a byte, short, or char, the result is also an int.
Order of operations
Precedence is the rule that decides which operator Java evaluates first. Multiplication, division, and remainder have higher precedence than addition and subtraction.
| Expression | Calculation | Result |
|---|---|---|
| `7 + 2 * 3` | Multiply first: `7 + 6` | `13` |
| `(7 + 2) * 3` | Add inside parentheses first: `9 * 3` | `27` |
| `8 - 6 / 2` | Divide first: `8 - 3` | `5` |
Parentheses are the symbols ( and ). They let you group part of an expression and make Java calculate it first.
Operators at the same precedence level are evaluated from left to right. This matters for division:
7 / 2 * 2is(7 / 2) * 2, so the result is6.7 / (2 * 2)is7 / 4, so the result is1.
Both use integer division. Add parentheses when they make your intended calculation clearer, even if Java would already use the order you want.
Division by zero
Dividing a whole number by zero causes an exception, which is an event that interrupts normal program execution. For example, integer division by zero produces an ArithmeticException. Integer remainder by zero does the same.
Floating-point division behaves differently. For example, 7.0 / 0.0 produces Infinity, and 0.0 / 0.0 produces NaN, meaning “not a number.” A program can continue after producing these values, so check that a divisor is valid when your calculation requires it.
Check your understanding
Exercise
What values do 9 / 4 and 9 / 4.0 produce?
Show answer
9 / 4 produces the int value 2. 9 / 4.0 produces the double value 2.25.
Exercise
If a and b are both byte variables, what is the type of a + b? Does the operation change either variable?
Show answer
The expression has type int. The operation does not change either variable.
Exercise
What are the value and type of 7 / 2 + 0.5?
Show answer
The value is 3.5 and the type is double. The integer division produces 3 first, then Java adds 0.5.