Java Math subtractExact() Method
Example
Subtract integers using subtractExact()
:
System.out.println(Math.subtractExact(12, 16));
System.out.println(Math.subtractExact(24060, 10000));
Try it Yourself »
Definition and Usage
The subtractExact()
method subtracts two integers and throws an exception if the subtraction causes an overflow. This prevents incorrect results that can occur from subtracting really large negative numbers.
Syntax
One of the following:
public static int subtractExact(int x, int y)
public static long subtractExact(long x, long y)
Parameter Values
Parameter | Description |
---|---|
x | Required. The number to subtract from. |
y | Required. The amount to subtract. |
Technical Details
Returns: | An int or long value representing the subtraction of two numbers. |
---|---|
Throws: | ArithmeticException - If the subtraction causes an overflow. |
Java version: | 1.8+ |
❮ Math Methods