BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Numbers and math

Java language

Integer overflow and exact arithmetic

An int that goes past 2,147,483,647 does not fail: it silently wraps around to a large negative number.

Learn these first

At a glance

Mental model

A car odometer that rolls over from 999999 to 000000 without warning.

What happens, in order
  1. 1Integer.MAX_VALUE + 1 is Integer.MIN_VALUEA product like age * 1_000_000_000 overflows even if the final result is stored in a long, because the multiplication happens in int first: write age * 1_000_000_000L.
  2. 2Math.addExact(a, b), multiplyExact, toIntExact(long) throw ArithmeticException on overflow, so a wrong total becomes a visible error
  3. 3% can be negative: -7 % 3 is -1Use Math.floorMod(-7, 3) (2) for an array index or a hash bucket.
  4. 4Math.abs(Integer.MIN_VALUE) is still negative, a famous trap in hash and random-index code
Remember
Watch out
Multiplying two ints and assigning to a longUsing the % result as an index without handling negativesComputing the average of two ints with (a + b) / 2
Example (highlighted lines explain it)
public class Totals {  static int safeTotal(int price, int qty) { return Math.multiplyExact(price, qty); }  static long bigTotal(int price, int qty) { return (long) price * qty; }  static int bucket(int hash, int buckets) { return Math.floorMod(hash, buckets); }  static int midpoint(int lo, int hi) { return lo + (hi - lo) / 2; }  static final int LIMIT = Integer.MAX_VALUE;}
  1. line 2 uses multiplyExact, the core of Integer overflow and exact arithmetic
  2. line 4 uses floorMod, the core of Integer overflow and exact arithmetic
Read the full explanation (about 1 min)

An int that goes past 2,147,483,647 does not fail: it silently wraps around to a large negative number. Math.addExact and friends throw an exception instead.

A mental model

A car odometer that rolls over from 999999 to 000000 without warning.

How it works

Integer.MAX_VALUE + 1 is Integer.MIN_VALUE. A product like age * 1_000_000_000 overflows even if the final result is stored in a long, because the multiplication happens in int first: write age * 1_000_000_000L.

Math.addExact(a, b), multiplyExact, toIntExact(long) throw ArithmeticException on overflow, so a wrong total becomes a visible error.

% can be negative: -7 % 3 is -1. Use Math.floorMod(-7, 3) (2) for an array index or a hash bucket.

Math.abs(Integer.MIN_VALUE) is still negative, a famous trap in hash and random-index code.

Going deeper

The midpoint formula (low + high) / 2 overflows for large arrays; use low + (high - low) / 2 or (low + high) >>> 1. This bug lived in the JDK binary search for years.

For values that can really grow (counters, money, ids) use long, BigInteger or LongAdder, and add an upper bound check on any number that comes from outside.

Overflow is a security issue too: a length calculation that wraps can allow an out-of-range read or write in languages without bounds checks and bypass size limits in Java.

Under the hood

The CPU simply discards the carry bit (two's complement arithmetic). The *Exact methods are JIT intrinsics that compile to the add instruction plus an overflow-flag check, so they are almost free.

Common mistakes

  • Multiplying two ints and assigning to a long.
  • Using the % result as an index without handling negatives.
  • Computing the average of two ints with (a + b) / 2.

How Unravel recognises it

When you paste code, Unravel looks for these signs of integer overflow and exact arithmetic. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Integer overflow and exact arithmetic?

An int that goes past 2,147,483,647 does not fail: it silently wraps around to a large negative number. Math.addExact and friends throw an exception instead.

What are the most common mistakes with Integer overflow and exact arithmetic?

Multiplying two ints and assigning to a long. Using the % result as an index without handling negatives. Computing the average of two ints with (a + b) / 2.

How can I see Integer overflow and exact arithmetic in real code?

Paste your own Java into Unravel and it will point out where integer overflow and exact arithmetic appears, explain how it works at the detail level you choose, and draw the flow of the code around it.