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
Call
What 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
Parameter
Description
x
The value to subtract from.
y
The value to subtract.
Return Value
Return Value
Description
int / long
The difference x - y, when it fits in the type.
Throws ArithmeticException
When 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:
Method
How it differs
x - y
No check: Integer.MIN_VALUE - 1 silently wraps to 2147483647.
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));
}
}
Output
380
-15
-9000000000
-2147483647
How It Works
A normal difference. 500 minus 120 is 380. Nothing is near the edge of the int range, so subtractExact simply returns it.
Negative results are fine. 10 minus 25 is -15; only results below -2147483648 count as overflow for an int.
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.
plain: 2147483647
MIN - 1: integer overflow
0 - MIN: integer overflow
-1 - MIN: 2147483647
long MIN - 1: long overflow
How It Works
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.
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.
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());
}
}
}
}
Output
elapsed 250000 ms
elapsed 650500 ms
bad timestamp -9223372036854775808: long overflow
How It Works
Newest minus oldest. 1700000250000 minus 1700000000000 is 250000 ms, and after the assignment last holds that reading for the next round.
Second step. 1700000900500 minus 1700000250000 leaves 650500 ms: each duration is measured from the previous good reading.
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.