Java Math subtractExact() Method

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

Definition

In Java, Math.subtractExact(x, y) returns x - y, but it throws an ArithmeticException if the difference does not fit in the type. The plain - operator would instead wrap around and give a wrong number without any warning.

It is handy for differences you cannot trust: time stamps from outside, counters, balances, and any values that might sit near the edge of the int or long range.

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.subtractExact(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass the value to subtract from first, then the value to subtract. Both arguments are int, or both are long.

Java
int left = Math.subtractExact(500, 120);       // 380
long gap = Math.subtractExact(end, start);       // elapsed time
Math.subtractExact(Integer.MIN_VALUE, 1);        // throws ArithmeticException
CallWhat it does
Math.subtractExact(10, 25)int overload: returns -15.
Math.subtractExact(10L, 25L)long overload: returns -15L.
Math.subtractExact(0, Integer.MIN_VALUE)Throws: the true answer 2147483648 is too big for an int.
Math.subtractExact(-1, Integer.MIN_VALUE)Returns 2147483647: it fits, so no exception.

Overflow can happen in both directions: a very negative number minus a positive one goes below the minimum, and a positive number minus a very negative one goes above the maximum.

Library declarations
Java
// java.lang.Math
public static int subtractExact(int x, int y)
public static long subtractExact(long x, long y)

Parameters

ParameterDescription
xThe value to subtract from.
yThe value to subtract.

Return Value

Return ValueDescription
int / longThe difference x - y, when it fits in the type.
Throws ArithmeticExceptionWhen the difference does not fit: integer overflow (int version) or long overflow (long version).

Compare with Similar Methods

Subtraction next to the other exact operations:

MethodHow it differs
x - yNo check: Integer.MIN_VALUE - 1 silently wraps to 2147483647.
addExact(x, y)Overflow-safe addition. subtractExact(x, y) acts like addExact(x, -y) without the negation problem.
negateExact(x)Overflow-safe sign flip, equal to subtractExact(0, x).
decrementExact(x)Overflow-safe x - 1.

Java Examples

Example 1 — Differences That Fit Behave Like the - Operator

Positive, negative, and long results, all inside the allowed range.

Java
public class MathSubtractExactBasics {
    public static void main(String[] args) {
        System.out.println(Math.subtractExact(500, 120));
        System.out.println(Math.subtractExact(10, 25));
        System.out.println(Math.subtractExact(-5_000_000_000L, 4_000_000_000L));
        System.out.println(Math.subtractExact(0, Integer.MAX_VALUE));
    }
}

How It Works

  1. A normal difference. 500 minus 120 is 380. Nothing is near the edge of the int range, so subtractExact simply returns it.
  2. Negative results are fine. 10 minus 25 is -15; only results below -2147483648 count as overflow for an int.
  3. Large long values. -5000000000 minus 4000000000 is -9000000000, far outside int range but well inside long range, so the long overload returns it. The last line, 0 minus 2147483647, lands exactly on -2147483647 and also fits.

Example 2 — Going Past MIN_VALUE and MAX_VALUE

Subtract from the smallest int, and from zero, to see which cases overflow.

Java
public class MathSubtractExactEdges {
    public static void main(String[] args) {
        int smallest = Integer.MIN_VALUE;
        System.out.println("plain:         " + (smallest - 1));
        try {
            Math.subtractExact(smallest, 1);
        } catch (ArithmeticException e) {
            System.out.println("MIN - 1:       " + e.getMessage());
        }
        try {
            Math.subtractExact(0, smallest);
        } catch (ArithmeticException e) {
            System.out.println("0 - MIN:       " + e.getMessage());
        }
        System.out.println("-1 - MIN:      " + Math.subtractExact(-1, smallest));
        try {
            Math.subtractExact(Long.MIN_VALUE, 1L);
        } catch (ArithmeticException e) {
            System.out.println("long MIN - 1:  " + e.getMessage());
        }
    }
}

How It Works

  1. Wrapping downwards. One step below -2147483648 wraps to the other end of the range, so the plain subtraction prints 2147483647, a huge positive number from a subtraction that should be negative.
  2. Two ways to overflow. MIN - 1 falls below the minimum, and 0 - MIN would need +2147483648, one more than the maximum; both throw integer overflow.
  3. A near miss that is fine. -1 minus -2147483648 is exactly 2147483647, the largest int, so it returns normally. The long version of the first case prints long overflow.

Example 3 — Elapsed Time Between Time Stamps

Subtract each new time stamp (in milliseconds) from the previous one, and reject a corrupt value instead of recording a nonsense duration.

Java
public class MathSubtractExactElapsed {
    public static void main(String[] args) {
        long last = 1_700_000_000_000L;
        long[] readings = {1_700_000_250_000L, 1_700_000_900_500L, Long.MIN_VALUE};
        for (long now : readings) {
            try {
                long elapsed = Math.subtractExact(now, last);
                System.out.println("elapsed " + elapsed + " ms");
                last = now;
            } catch (ArithmeticException e) {
                System.out.println("bad timestamp " + now + ": " + e.getMessage());
            }
        }
    }
}

How It Works

  1. Newest minus oldest. 1700000250000 minus 1700000000000 is 250000 ms, and after the assignment last holds that reading for the next round.
  2. Second step. 1700000900500 minus 1700000250000 leaves 650500 ms: each duration is measured from the previous good reading.
  3. The bad value is caught. Long.MIN_VALUE minus a positive time stamp falls below the smallest long; a plain - would have produced a huge positive duration, but subtractExact throws and the loop reports the problem.

Frequently Asked Questions

It returns x - y for two ints or two longs, but throws ArithmeticException if the difference does not fit in the type, instead of silently wrapping around.
A result below the minimum or above the maximum of the type. For int that means below -2147483648 or above 2147483647, for example Integer.MIN_VALUE - 1 or 0 - Integer.MIN_VALUE.
"integer overflow" for the int version and "long overflow" for the long version.
Almost, but not for y equal to MIN_VALUE: negating MIN_VALUE overflows, while subtractExact(-1, Integer.MIN_VALUE) correctly returns 2147483647.
Java 8, together with addExact, multiplyExact, incrementExact, decrementExact, negateExact, and toIntExact.

Next: tan()

Tangent of an angle given in radians.

tan() 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.

12 people found this page helpful