Java Reference ยท java.lang
Java Math.addExact() and the other exact methods
Reference for Java's overflow-checked arithmetic: addExact, subtractExact, multiplyExact, incrementExact, negateExact, toIntExact and absExact.
Ordinary int and long arithmetic in Java wraps around silently: Integer.MAX_VALUE + 1 is a large negative number and no error is reported. The *Exact methods in Math do the same calculation but throw an ArithmeticException when the true result does not fit in the type, so the bug becomes visible instead of corrupting data.
They exist for int and long (and some also for mixed long/int arguments). Use them wherever a wrong total would be worse than a crash: balances, sizes, array offsets, quantities.
int sum = Math.addExact(2_000_000_000, 2_000_000_000); // throws ArithmeticException
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
static int addExact(int x, int y) static long addExact(long x, long y) static int subtractExact(int x, int y) static long subtractExact(long x, long y) static int multiplyExact(int x, int y) static long multiplyExact(long x, long y) static long multiplyExact(long x, int y) static int incrementExact(int a) static int decrementExact(int a) static int negateExact(int a) static int absExact(int a) static int toIntExact(long value)
long versions of incrementExact, decrementExact, negateExact and absExact also exist.
| Method | Overflows when |
|---|---|
addExact, subtractExact |
the true sum or difference is outside the type's range |
multiplyExact |
the true product is outside the range |
incrementExact, decrementExact |
the argument is MAX_VALUE or MIN_VALUE |
negateExact, absExact |
the argument is MIN_VALUE (no positive counterpart) |
toIntExact |
the long is outside the int range |
Returns: the exact result.
Throws: ArithmeticException with the message integer overflow or long overflow. absExact (Java 15) uses a longer message saying that MIN_VALUE has no positive counterpart.
Usage notes
- Overload selection matters.
Math.addExact(a, b)with twointvalues checks against theintrange, even if you assign the result to along. Make one operandlongfirst:Math.addExact((long) a, b). - Mixed
long/intmultiply.multiplyExact(long, int)was added in Java 9; also seemultiplyFull(int, int), which returns the exactlongproduct and never overflows. - Division.
/only overflows forMIN_VALUE / -1. Java 18 addeddivideExact,floorDivExactandceilDivExactfor that case. - Cost. Modern JVMs compile these methods to a plain operation plus an overflow check, so they are cheap enough for almost any loop.
- Floating point has no exact variants.
doublearithmetic overflows to infinity instead of throwing. For unlimited size useBigInteger. - Catching.
ArithmeticExceptionis unchecked. Catch it at a boundary where you can report bad input; do not use it for ordinary control flow. - Compound assignment.
x += ynever uses the exact methods. Writex = Math.addExact(x, y)to opt in.
Related
- Integer overflow,
Math,Integer,Long,BigInteger - Lessons: Primitive types, Big numbers