Java Math addExact() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

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 addExact throws 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
CallWhat 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

ParameterDescription
xThe first value to add.
yThe second value to add.

Return Value

Return ValueDescription
int / longThe sum x + y, when it fits in the type.
Throws ArithmeticExceptionWhen 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:

MethodHow it differs
x + yNo check: wraps around silently, so Integer.MAX_VALUE + 1 is -2147483648.
subtractExact(x, y)The same overflow guard for subtraction.
multiplyExact(x, y)The same overflow guard for multiplication.
toIntExact(v)Brings a long sum back to int, throwing if it does not fit.

Java Examples

Example 1 — Normal Sums Work Like the + Operator

When the result fits, addExact gives exactly what + gives. Both overloads are shown.

Java
public class MathAddExactBasics {
    public static void main(String[] args) {
        System.out.println(Math.addExact(1200, 350));
        System.out.println(Math.addExact(-40, 15));
        System.out.println(Math.addExact(3_000_000_000L, 4_000_000_000L));
        System.out.println(Math.addExact(5_000_000_000L, 7));
    }
}

How It Works

  1. Plain int sums. 1200 plus 350 is 1550, and -40 plus 15 is -25. Both answers fit comfortably in an int, so no exception is thrown.
  2. Too big for int, fine for long. 3 billion is above the int limit of 2147483647, so the L suffix is needed; the long overload then returns 7000000000.
  3. Mixed arguments widen. In addExact(5_000_000_000L, 7) the int 7 is widened to long, so the long overload runs and returns 5000000007.

Example 2 — Silent Wrap-Around vs a Clear Exception

Add 1 to the largest int and see how each approach reacts, then widen to long to get the true answer.

Java
public class MathAddExactOverflow {
    public static void main(String[] args) {
        int big = Integer.MAX_VALUE;
        System.out.println("plain +:    " + (big + 1));
        try {
            System.out.println("addExact:   " + Math.addExact(big, 1));
        } catch (ArithmeticException e) {
            System.out.println("int threw:  " + e.getMessage());
        }
        try {
            Math.addExact(Long.MAX_VALUE, 1L);
        } catch (ArithmeticException e) {
            System.out.println("long threw: " + e.getMessage());
        }
        System.out.println("widened:    " + Math.addExact((long) big, 1));
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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);
    }
}

How It Works

  1. Two safe additions. 0 plus 900000000, then plus 800000000, gives 1700000000, which is still below 2147483647.
  2. 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.
  3. 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.

Next: asin()

Arc sine: the angle in radians (-pi/2 to pi/2) whose sine is the given value.

asin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful