skip to content

How does integer division work in Java, and how does division-by-zero behave for integers versus floating-point?

level: middleimportance: must knowfreq 65%

answer

  1. int / int -> truncate toward zero
  2. -7/2 == -3 (toward zero), Math.floorDiv -> -4
  3. int /0 throws ArithmeticException
  4. double /0.0 -> Infinity / NaN, never throws
  5. (double) sum / count to fix integer-average bug
  6. Integer.MIN_VALUE / -1 overflows

basics

~20 s

When both operands are whole numbers, / drops the fraction and rounds toward zero, so 7/2 is 3 and -7/2 is -3. Dividing an integer by 0 throws an error; dividing a double by 0.0 gives Infinity or NaN.

solid answer

~40 s

If both operands of / are integer types, Java performs integer division: it computes the exact mathematical quotient and then truncates toward zero, discarding any fraction. So 7/2 is 3, and -7/2 is -3 (not -4) because truncation rounds toward zero, not toward negative infinity. The fractional part you lost is exactly what % returns. Dividing an integer by zero throws ArithmeticException ("/ by zero") at runtime because there is no integer to represent the result. Floating-point division never throws: x/0.0 yields +Infinity, a negative numerator gives -Infinity, and 0.0/0.0 yields NaN. To get a real fraction from integer operands you must promote one to floating point, e.g. (double) a / b. A notorious overflow case is Integer.MIN_VALUE / -1, which overflows int range and wraps to Integer.MIN_VALUE.

code

java · 12 lines
java
System.out.println(7 / 2);        // 3
System.out.println(-7 / 2);       // -3  (toward zero, not -4)
System.out.println(Math.floorDiv(-7, 2)); // -4 (floor)

int sum = 7, count = 2;
System.out.println(sum / count);          // 3   (bug for an average)
System.out.println((double) sum / count); // 3.5 (cast one operand)

System.out.println(5.0 / 0.0);    // Infinity
System.out.println(-5.0 / 0.0);   // -Infinity
System.out.println(0.0 / 0.0);    // NaN
// System.out.println(5 / 0);     // throws ArithmeticException: / by zero

go deeper

for a junior

Knows 7/2 is 3 because the fraction is dropped, and that dividing an int by 0 crashes the program.

for a middle

Explains truncation toward zero for negatives, the int-vs-double divide-by-zero split, and the (double) cast fix for averages.

for a senior

Adds floorDiv vs truncation, the quotient/remainder identity, NaN/Infinity semantics, and the MIN_VALUE/-1 overflow case.

for a principal

Sets team conventions (e.g. prefer Math.*Exact for untrusted inputs, BigDecimal for money) and reasons about correctness/precision risk across a codebase.

## Setup: integer vs floating-point types Java numbers come in two families. **Integer types** (`byte`, `short`, `int`, `long`, and `char`) hold whole numbers only. **Floating-point types** (`float`, `double`) hold numbers with a fractional part, using the IEEE 754 representation. The `/` operator behaves differently depending on the **operand types**, because Java first applies **numeric promotion**: if *both* operands end up as integer types, you get **integer division**; if *either* operand is `float` or `double`, you get **floating-point division**. ## Integer division: truncate toward zero Integer division computes the exact quotient and then **drops the fractional part** — equivalently, it **truncates toward zero** (also called *rounding toward zero*). Examples: - `7 / 2` -> exact 3.5 -> truncate -> `3` - `-7 / 2` -> exact -3.5 -> truncate toward zero -> `-3` (NOT -4) - `8 / 3` -> 2.66… -> `2` 'Toward zero' matters for negatives: it chops the fraction, so a negative result moves *up* toward zero, not down. This differs from mathematical 'floor' division (used in some other languages), where `-7 / 2` would be `-4`. Java's `/` is truncation; if you want floor behaviour use `Math.floorDiv(a, b)`. The discarded fraction is exactly what `%` gives back, so the identity holds: `(a / b) * b + (a % b) == a` for integers (when b != 0). ## Division by zero This is the classic gotcha because the two families behave oppositely: - **Integer divide by zero** (`5 / 0` or `5 % 0`): there is no integer that can represent the result, so Java throws **`ArithmeticException`** with message `/ by zero` at runtime. It is *not* a compile error (unless the divisor is a constant zero the compiler can prove). - **Floating-point divide by zero** (`5.0 / 0.0`): never throws. IEEE 754 defines special values: a positive numerator gives `+Infinity`, a negative numerator gives `-Infinity`, and `0.0 / 0.0` gives `NaN` ('Not a Number'). `Double.isInfinite` / `Double.isNaN` test for these. ## Getting a real fraction from integers `double avg = sum / count;` is a frequent bug: if `sum` and `count` are `int`, the division happens in `int` first and the fraction is already gone before the widening to `double`. Fix by promoting an operand: `(double) sum / count` (the cast binds tighter than `/`, so only `sum` is cast and the whole division becomes floating-point). ## Overflow corner case Integer arithmetic wraps silently rather than throwing. The one division case that overflows is `Integer.MIN_VALUE / -1` (and the `long` analogue): the true result `2147483648` exceeds `Integer.MAX_VALUE`, so it wraps back to `Integer.MIN_VALUE`. `Math.divideExact` (Java 18+) throws on this instead. Knowing these rules lets you predict both the value and whether an expression can blow up.

  • Why is `-7 / 2` equal to -3 in Java but -4 in some other languages?
    Java's integer / truncates toward zero, chopping the fraction, so -3.5 becomes -3. Python and others use floor division, rounding toward negative infinity, giving -4. Use Math.floorDiv in Java if you need the floor behaviour.
  • How would you safely detect integer division overflow?
    Either guard the known case (numerator == Integer.MIN_VALUE && denominator == -1) before dividing, or use Math.divideExact(a, b) (Java 18+), which throws ArithmeticException on overflow instead of wrapping.

saying these in an interview costs you the question

  • Saying integer division rounds (it truncates toward zero, never rounds half-up)
  • Claiming -7/2 is -4 in Java
  • Saying double/0.0 throws an exception
  • Writing `double avg = sum / count;` and expecting a fraction from int operands
  • Forgetting Integer.MIN_VALUE / -1 overflows

context