3.2 Arithmetic, Division, and Remainders
Prerequisites: Expressions, Assignments, and Evaluation Order
Estimated time: 25 minutes
You are ready to continue when: You can predict integer division, remainders, overflow, and side effects of increment operators
Basic Arithmetic and Types
int left = 10;
int right = 3;
System.out.println(left + right); // 13
System.out.println(left - right); // 7
System.out.println(left * right); // 30
System.out.println(left / right); // 3
System.out.println(left % right); // 1When participating in ordinary arithmetic, byte, short, and char are typically promoted to int. Expressions may also overflow, and types do not automatically upgrade due to the result being too large.
Integer Division Truncating Toward Zero
System.out.println(10 / 3); // 3
System.out.println(-10 / 3); // -3
System.out.println(10 / -3); // -3
System.out.println(-10 / -3); // 3“Drop the decimal part” is an informal shortcut. The precise rule, including negative operands, is to truncate the quotient toward zero.
When at least one operand is a floating-point type, a floating-point division is performed:
System.out.println(10 / 3.0); // 3.3333333333333335
System.out.println((double) 10 / 3); // 3.3333333333333335Assigning to double does not propagate to the right side of the operation:
double wrong = 10 / 3; // Compute int 3 first, then widen to 3.0
double right = 10 / 3.0;Integer division by zero throws ArithmeticException; floating-point division by positive zero or negative zero produces infinity or NaN according to IEEE 754:
System.out.println(1.0 / 0.0); // Infinity
System.out.println(0.0 / 0.0); // NaNThere is also an integer boundary: the mathematical result of Integer.MIN_VALUE / -1 exceeds int, and Java returns Integer.MIN_VALUE. Math.floorDiv does not detect this overflow either. In Java 17, check this input pair explicitly or convert an operand to long before dividing.
% is the remainder, not necessarily non-negative modulo
In Java, the remainder satisfies:
(a / b) * b + (a % b) == aThis uses Java integer arithmetic and requires a nonzero divisor. Even when MIN_VALUE / -1 overflows, the identity holds under Java’s wraparound rules. A nonzero remainder has the sign of the dividend:
System.out.println(7 % 3); // 1
System.out.println(-7 % 3); // -1
System.out.println(7 % -3); // 1Therefore, to check for an odd number, you should write:
boolean odd = value % 2 != 0;value % 2 == 1 will miss negative odd numbers.
For a positive modulus, use this to obtain an index in 0..modulus-1:
int wrapped = Math.floorMod(index, 3);System.out.println(-1 % 3); // -1
System.out.println(Math.floorMod(-1, 3)); // 2Pagination also requires defining the numbering convention in advance. If the entries start from 0, index / pageSize is the page index; if the business numbering starts from 1, the formula is typically (number - 1) / pageSize. Do not just paste one % and call it a general pagination.
Increment and Decrement Are Side-Effectful Expressions
int count = 5;
int before = count++; // before=5, count=6
int after = ++count; // count=7, after=7- The value of an expression with a postfix
count++is the value before the variable is incremented, and then the variable is increased by one; - The value of an expression with a prefix
++countis the value after the variable is modified, and the expression value is the modified value.
The count++ is very common in loop updates. Do not read and write the same variable multiple times within the same complex expression:
int result = count++ + ++count; // Rules are defined but have high maintenance costsSplitting it can preserve the intent:
int first = count;
count++;
count++;
int second = count;
int result = first + second;Overflow and exact Methods
int area = width * height;Even if width and height are both valid, the product may overflow. When failure is required rather than wrapping:
int area = Math.multiplyExact(width, height);If you first cast to long:
long area = (long) width * height;The cast must come before the multiplication; long area = width * height will first compute using int and may have already overflowed.
Professional Reference
| Scenario | Rule |
|---|---|
| Integer division | Truncate toward zero |
| Floating-point division by zero | Infinity or NaN, not throw integer division by zero exception |
| Remainder sign | Same as the dividend |
| Non-negative loop index | Math.floorMod with a positive modulus |
| Negative parity | value % 2 != 0 |
Multiplication promoted to long | Convert one operand before operation |
| To detect overflow | Math.*Exact or explicit boundary check |
++/-- | Simple update is available, avoid mixing with other side effects |
Before Moving On
Use -10 / 3, -7 % 3, and Math.floorMod(-1, 3) to check if you can accurately write the rules for negative numbers; then explain why long result = intValue * otherInt might still overflow first.
Arithmetic produces values, but programs also need to combine conditions.
→ Next: Comparisons, Equality, and Short-Circuit Logic
Specification: JLS §15.17: Multiplication, Division, and Remainder, §15.14.2: Postfix Increment.