Java Math toIntExact() Method

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

Definition

In Java, Math.toIntExact(value) converts a long to an int only if the number really fits. If it is below -2147483648 or above 2147483647, it throws an ArithmeticException.

The usual (int) cast never complains: it cuts off the high bits and gives you a wrong number. toIntExact is the safe version, ideal for array lengths, list sizes, and APIs that only accept an int.

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.toIntExact(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass a long; you get an int back. Wrap the call in try if the value comes from outside your control.

Java
int size = Math.toIntExact(1500L);                // 1500
int[] data = new int[Math.toIntExact(count)];     // safe array length
Math.toIntExact(3_000_000_000L);                  // throws ArithmeticException
CallWhat it does
Math.toIntExact(42L)Returns the int 42.
Math.toIntExact(-2_147_483_648L)Returns -2147483648: the smallest int still fits.
Math.toIntExact(2_147_483_648L)Throws ArithmeticException ("integer overflow").
Math.toIntExact(a + b)Add as long, then convert back only if the sum fits.

Only a long parameter exists. An int argument is simply widened to long and returned unchanged. To go the other way, from int to long, no method is needed: a plain assignment widens safely.

Library declarations
Java
// java.lang.Math
public static int toIntExact(long value)

Parameters

ParameterDescription
valueThe long value to convert.

Return Value

Return ValueDescription
intThe same number as an int, when it is between Integer.MIN_VALUE and Integer.MAX_VALUE.
Throws ArithmeticExceptionWhen the value is outside the int range. The message is integer overflow.

Compare with Similar Methods

Ways to turn a long into an int:

MethodHow it differs
(int) valueCuts off the high bits silently: (int) 5_000_000_000L is 705032704.
clamp(value, min, max)Caps the value at an int limit instead of throwing. Java 21+.
addExact(a, b)A guarded add that often comes right before a toIntExact.
Long.valueOf(v).intValue()Same wrapping behaviour as the cast.

Java Examples

Example 1 — Values That Fit, and the First One That Does Not

Convert several long values at and around the edges of the int range.

Java
public class MathToIntExactBasics {
    public static void main(String[] args) {
        System.out.println(Math.toIntExact(42L));
        System.out.println(Math.toIntExact(-2_147_483_648L));
        System.out.println(Math.toIntExact(2_147_483_647L));
        try {
            Math.toIntExact(2_147_483_648L);
        } catch (ArithmeticException e) {
            System.out.println("2147483648 -> " + e.getMessage());
        }
    }
}

How It Works

  1. Small values. 42L converts to the int 42, with no change in value.
  2. Both ends fit. -2147483648 and 2147483647 are exactly Integer.MIN_VALUE and Integer.MAX_VALUE, so both are returned unchanged.
  3. One step too far. 2147483648 is one more than the largest int, so toIntExact throws and the catch block prints the message integer overflow.

Example 2 — File Sizes: the (int) Cast vs toIntExact

Compare the two conversions for several file sizes in bytes.

Java
public class MathToIntExactCast {
    public static void main(String[] args) {
        long[] sizes = {1_500L, 2_147_483_647L, 2_147_483_648L, 5_000_000_000L};
        for (long size : sizes) {
            String exact;
            try {
                exact = String.valueOf(Math.toIntExact(size));
            } catch (ArithmeticException e) {
                exact = e.getMessage();
            }
            System.out.println(size + " -> cast " + (int) size + ", exact " + exact);
        }
    }
}

How It Works

  1. They agree when the value fits. 1500 and 2147483647 convert to the same int either way.
  2. The cast invents numbers. 2147483648 becomes -2147483648 and 5000000000 becomes 705032704 (5000000000 minus 4294967296), so a program using the cast would treat a 5 GB file as about 705 MB.
  3. toIntExact reports the problem. For both oversized values it throws; the catch block stores the message, so the output line shows integer overflow instead of a wrong size.

Example 3 — Milliseconds to Whole Seconds as an int

Convert durations in milliseconds to seconds that must fit an int field, and reject one that is far too long.

Java
public class MathToIntExactSeconds {
    public static void main(String[] args) {
        long[] millis = {90_000L, 86_400_000L, 9_000_000_000_000L};
        for (long ms : millis) {
            try {
                int seconds = Math.toIntExact(ms / 1000);
                System.out.println(ms + " ms -> " + seconds + " s");
            } catch (ArithmeticException e) {
                System.out.println(ms + " ms -> too long: " + e.getMessage());
            }
        }
    }
}

How It Works

  1. Divide first, convert second. ms / 1000 is done in long, giving 90 for the first value, and toIntExact then turns it into an int.
  2. A full day. 86400000 milliseconds is 86400 seconds, one whole day, which is tiny compared with the int limit.
  3. An enormous duration. 9000000000000 ms is 9000000000 seconds, more than 2147483647, so the conversion throws and the loop prints a clear message.

Frequently Asked Questions

It converts a long to an int and returns it if the value fits. If the value is outside the int range it throws ArithmeticException with the message "integer overflow".
The cast silently keeps only the low 32 bits, so (int) 5_000_000_000L gives 705032704. toIntExact throws an exception instead of returning a wrong number.
No. It takes a long, which is already a whole number. For a double, round it first with Math.round and then convert the resulting long.
The same "integer overflow". Both values above Integer.MAX_VALUE and values below Integer.MIN_VALUE produce that message.
Whenever a long must become an int, for example for array sizes, collection indexes, or older APIs that accept only int, and a wrong value would be a bug.

Next: toRadians()

Convert an angle from degrees to radians.

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

7 people found this page helpful