Skip to content
Tutorials Center
My learning

Topics

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.

Java
int sum = Math.addExact(2_000_000_000, 2_000_000_000); // throws ArithmeticException

Try it

Java Silent wrap-around versus an exception

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.

Java Narrowing and a safe sum

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

Java
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 two int values checks against the int range, even if you assign the result to a long. Make one operand long first: Math.addExact((long) a, b).
  • Mixed long/int multiply. multiplyExact(long, int) was added in Java 9; also see multiplyFull(int, int), which returns the exact long product and never overflows.
  • Division. / only overflows for MIN_VALUE / -1. Java 18 added divideExact, floorDivExact and ceilDivExact for 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. double arithmetic overflows to infinity instead of throwing. For unlimited size use BigInteger.
  • Catching. ArithmeticException is unchecked. Catch it at a boundary where you can report bad input; do not use it for ordinary control flow.
  • Compound assignment. x += y never uses the exact methods. Write x = Math.addExact(x, y) to opt in.