Java Math divideExact() Method

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

Definition

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

ParameterDescription
xThe dividend (the number to divide).
yThe divisor (the number to divide by). Must not be zero.

Return Value

Return ValueDescription
int / longThe 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:

MethodHow it differs
x / ySame quotient, but Integer.MIN_VALUE / -1 silently stays -2147483648.
floorDivExact(x, y)Rounds the quotient down and also throws on overflow. Java 18+.
ceilDivExact(x, y)Rounds the quotient up and also throws on overflow. Java 18+.
floorDiv(x, y)Rounds down, but wraps on MIN_VALUE / -1 instead of throwing.

Java Examples

Example 1 — Division Truncates Toward Zero

Ordinary quotients with positive and negative operands, plus a long division. This example needs Java 18 or newer.

Java
public class MathDivideExactBasics {
    public static void main(String[] args) {
        System.out.println("17 / 5  = " + Math.divideExact(17, 5));
        System.out.println("-17 / 5 = " + Math.divideExact(-17, 5));
        System.out.println("17 / -5 = " + Math.divideExact(17, -5));
        System.out.println("long    = " + Math.divideExact(7_000_000_000L, 2L));
    }
}

How It Works

  1. The remainder is dropped. 17 divided by 5 is 3.4, and integer division keeps only the whole part, 3.
  2. Toward zero, not down. -3.4 loses its decimals to become -3, not -4; this is the same rule as the plain / operator and it differs from floorDivExact.
  3. A long quotient. 7000000000 is above the int range, so the long overload is used and returns 3500000000.

Example 2 — MIN_VALUE / -1 and Division by Zero

The two inputs that make divideExact throw, compared with the plain operator. Java 18+.

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

How It Works

  1. The one overflowing quotient. -2147483648 divided by -1 should be +2147483648, which does not fit in an int, so plain / wraps back to -2147483648.
  2. Exact reports it. divideExact throws integer overflow for the int case and long overflow for Long.MIN_VALUE divided by -1.
  3. 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());
            }
        }
    }
}

How It Works

  1. A clean split. 1200 shared among 4 groups is exactly 300 each.
  2. 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.
  3. 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.

Next: exp()

Euler's number e raised to a power.

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

15 people found this page helpful