In Java, Math.addExact(x, y) adds two whole numbers and returns the sum, just like x + y. The difference shows up when the answer is too big: the plain + operator silently wraps around to a wrong number, while addExactthrows an ArithmeticException.
Use it where a wrong total would be a real bug: adding prices in cents, scores, quantities, or byte counts. You get either the correct sum or a clear error, never a quiet mistake.
A static method of the java.lang.Math class, added in Java 8 (JDK 1.8). Call it on the class name, as in Math.addExact(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass two int values or two long values. The result has the same type. Wrap the call in try if overflow is possible.
Java
int total = Math.addExact(1200, 350); // 1550
long big = Math.addExact(3_000_000_000L, 4_000_000_000L); // 7000000000
Math.addExact(Integer.MAX_VALUE, 1); // throws ArithmeticException
Call
What it does
Math.addExact(20, 22)
int overload: returns 42.
Math.addExact(20L, 22L)
long overload: returns 42L.
Math.addExact(Integer.MAX_VALUE, 1)
Throws ArithmeticException ("integer overflow").
Math.addExact((long) a, b)
Widen one operand to long to get a safe long sum of two ints.
There is no float or double version: floating-point addition never overflows into a wrong integer, it goes to Infinity. Mixing an int and a long picks the long overload.
Library declarations
Java
// java.lang.Math
public static int addExact(int x, int y)
public static long addExact(long x, long y)
Parameters
Parameter
Description
x
The first value to add.
y
The second value to add.
Return Value
Return Value
Description
int / long
The sum x + y, when it fits in the type.
Throws ArithmeticException
When the sum does not fit. The message is integer overflow for the int version and long overflow for the long version.
Compare with Similar Methods
How addition compares with its overflow-safe relatives:
Method
How it differs
x + y
No check: wraps around silently, so Integer.MAX_VALUE + 1 is -2147483648.
plain +: -2147483648
int threw: integer overflow
long threw: long overflow
widened: 2147483648
How It Works
The silent mistake. 2147483647 + 1 needs 32 bits plus one more, so the plain + wraps around to the smallest int, -2147483648, and the program carries on with a wrong value.
Two messages. The int call fails first and prints integer overflow; because the exception skips the println, "addExact:" never appears. The long version reports long overflow.
Widen to get the real answer. Casting big to long selects the long overload, which has room for 2147483648, so no exception is thrown.
Example 3 — A Running Total of Prices in Cents
Add three big order amounts one by one. The loop stops at the first amount that would overflow an int instead of storing a nonsense total.
Java
public class MathAddExactCart {
public static void main(String[] args) {
int[] pricesInCents = {900_000_000, 800_000_000, 700_000_000};
int total = 0;
for (int price : pricesInCents) {
try {
total = Math.addExact(total, price);
System.out.println("total = " + total);
} catch (ArithmeticException e) {
System.out.println("stopped at " + price + ": " + e.getMessage());
break;
}
}
System.out.println("final total = " + total);
}
}
Output
total = 900000000
total = 1700000000
stopped at 700000000: integer overflow
final total = 1700000000
How It Works
Two safe additions. 0 plus 900000000, then plus 800000000, gives 1700000000, which is still below 2147483647.
The third one is refused. 1700000000 plus 700000000 would be 2400000000. With plain + that wraps to -1894967296; addExact throws instead and the catch block ends the loop with break.
The total stays trustworthy. Because the failing call never assigned anything, total still holds the last correct sum, 1700000000.
Frequently Asked Questions
It adds two ints or two longs and returns the sum. If the sum does not fit in the type, it throws ArithmeticException instead of wrapping around like the + operator.
The int version throws ArithmeticException with the message "integer overflow", and the long version throws it with "long overflow".
Java 8. On Java 7 and older you have to check for overflow yourself, for example by adding as long and comparing with Integer.MAX_VALUE.
No. It is an unchecked exception, so the compiler does not require a try block. Catch it where you can handle it, such as rejecting bad input or stopping a loop.
Widen one of them to long first, for example Math.addExact((long) a, b). The long sum of two ints always fits, so no exception can occur.