Java Math.divideExact(): Avoiding Range Overflow Errors

In Java, we can divide two numbers using the division operator ('/') as part of simple mathematics. Beginning Java 18, we can use Math.divideExact() method that throws an ArithmeticException if the result overflows.

1. The Overflow Problem

Let us begin with a simple example of division in Java. We are dividing 100 by -1. The result is -100. This is a straightforward use case that works as expected.

int result = 100 / -1;
System.out.println(result);   // -100

Let’s make it more complex and use division on large numbers. We are dividing the Integer.MIN_VALUE value with -1 which results in -2,147,483,648. This is an incorrect result.

int maxInteger = Integer.MAX_VALUE;   //  2,147,483,647
int minInteger = Integer.MIN_VALUE;   // -2,147,483,648    

int quotient = minInteger / -1;       // -2,147,483,648

The above result is incorrect because it should be the +2,147,483,648 which doesn’t fit in the int domain. The int domain was overflowed and resulted in |Integer.MIN_VALUE| > |Integer.MAX_VALUE| (comparing absolute values).

The same problem will also appear for long datatype when we use Long.MIN_VALUE. The following division is also incorrect and should have produced a positive number.

long y = Long.MIN_VALUE;    // -9,223,372,036,854,775,808
long quotient = y / -1;     // -9,223,372,036,854,775,808

2. The Solution: Math.divideExact()

Starting with JDK 18, the Math class has two additional divideExact() methods – one for int and the second for long datatype.

public static int divideExact(int x, int y)
public static long divideExact(long x, long y)

These methods return the quotient of the arguments as expected. Additionally, these methods throw an ArithmeticException:

  • if the result overflows
  • if y (divisor) is zero

To be precise, the overflow occurs in this method ONLY IF x is Integer.MIN_VALUE and y is -1. In such cases, these methods throw ArithmeticException instead of returning a misleading result.

int quotientExactFine = Math.divideExact(4, -1);

int quotientExactEx = Math.divideExact(x, -1);

The program output:


Exception in thread "main" java.lang.ArithmeticException: integer overflow
  at java.base/java.lang.Math.divideExact(
  at com.howtodoinjava.core.basic.math.MathDivideExact.main(

3. Conclusion

As discussed in this tutorial, the Math.divideExact() method is very useful if the division result is prone to overflowing the int or long (as Integer.MIN_VALUE/Long.MIN_VALUE overflows the positive range).

It is worth remembering to use BigInteger and BigDecimal classes when working with large numbers that support values in the range -2Integer.MAX_VALUE (exclusive) to +2Integer.MAX_VALUE (exclusive) and practically it is impossible to cross this limit in a real-world application.

Happy Learning !!

Source Code on Github


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode