In Java, Math.divideExact(x, y) performs ordinary integer division, the same as x / y, and the result is rounded toward zero in the same way. The only difference is how it handles the single overflowing case: dividing the smallest value by -1, where the plain / operator quietly returns a wrong, negative number.
With divideExact, that case throws an ArithmeticException. Division by zero throws too, just as it does with /.
A static method of the java.lang.Math class, added in Java 18 (JDK 18). Call it on the class name, as in Math.divideExact(...). No import is needed, because classes in java.lang are always visible. Requires Java 18 or newer.
Syntax
Pass two int values or two long values. The result is truncated toward zero, like /.
Java
int a = Math.divideExact(17, 5); // 3
long b = Math.divideExact(7_000_000_000L, 2L); // 3500000000
Math.divideExact(Integer.MIN_VALUE, -1); // throws ArithmeticException
Call
What it does
Math.divideExact(17, 5)
int overload: returns 3 (the remainder 2 is dropped).
Math.divideExact(-17, 5)
Returns -3: rounds toward zero, not down.
Math.divideExact(10, 0)
Throws ArithmeticException ("/ by zero").
Math.divideExact(Integer.MIN_VALUE, -1)
Throws ArithmeticException ("integer overflow").
For every input except MIN_VALUE / -1 and a zero divisor, divideExact(x, y) returns exactly x / y. If you want the quotient rounded down or up instead of toward zero, see floorDivExact and ceilDivExact.
Library declarations
Java
// java.lang.Math (Java 18+)
public static int divideExact(int x, int y)
public static long divideExact(long x, long y)
Parameters
Parameter
Description
x
The dividend (the number to divide).
y
The divisor (the number to divide by). Must not be zero.
Return Value
Return Value
Description
int / long
The quotient x / y, rounded toward zero.
Throws ArithmeticException
/ by zero if y is 0; integer overflow or long overflow for MIN_VALUE / -1.
Compare with Similar Methods
Integer division options:
Method
How it differs
x / y
Same quotient, but Integer.MIN_VALUE / -1 silently stays -2147483648.
plain /: -2147483648
int overflow: integer overflow
long overflow: long overflow
divide by 0: / by zero
How It Works
The one overflowing quotient. -2147483648 divided by -1 should be +2147483648, which does not fit in an int, so plain / wraps back to -2147483648.
Exact reports it.divideExact throws integer overflow for the int case and long overflow for Long.MIN_VALUE divided by -1.
Zero is still zero. Dividing 10 by 0 throws with the standard message / by zero, exactly as the / operator does.
Example 3 — Split a Total Between Groups, Even a Bad Count
Share 1200 among a list of group sizes; the invalid size 0 is reported and the loop continues. Java 18+.
Java
public class MathDivideExactSplit {
public static void main(String[] args) {
int total = 1200;
int[] groups = {4, 0, -1};
for (int g : groups) {
try {
System.out.println(total + " / " + g + " = " + Math.divideExact(total, g));
} catch (ArithmeticException e) {
System.out.println(total + " / " + g + " -> " + e.getMessage());
}
}
}
}
Output
1200 / 4 = 300
1200 / 0 -> / by zero
1200 / -1 = -1200
How It Works
A clean split. 1200 shared among 4 groups is exactly 300 each.
Zero groups. A group size of 0 makes the division meaningless, so the call throws / by zero and the catch block prints it instead of stopping the program.
Negative divisors are legal. Dividing 1200 by -1 gives -1200 with no exception, since only MIN_VALUE divided by -1 overflows.
Frequently Asked Questions
It divides two ints or two longs like the / operator, rounding toward zero, but throws ArithmeticException when the result overflows, which happens only for MIN_VALUE divided by -1.
Java 18. On older versions use x / y and check for MIN_VALUE with a divisor of -1 yourself.
"/ by zero" when the divisor is 0, "integer overflow" for Integer.MIN_VALUE / -1, and "long overflow" for Long.MIN_VALUE / -1L.
No. Math.divideExact(17, 5) returns 3. The word Exact refers to overflow only, not to whether the division leaves a remainder.
It is -2147483648. The true answer, 2147483648, does not fit in an int, so the operator wraps around without any error.