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
Call
What 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
Parameter
Description
value
The long value to convert.
Return Value
Return Value
Description
int
The same number as an int, when it is between Integer.MIN_VALUE and Integer.MAX_VALUE.
Throws ArithmeticException
When the value is outside the int range. The message is integer overflow.
Compare with Similar Methods
Ways to turn a long into an int:
Method
How it differs
(int) value
Cuts off the high bits silently: (int) 5_000_000_000L is 705032704.
They agree when the value fits. 1500 and 2147483647 convert to the same int either way.
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.
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());
}
}
}
}
Output
90000 ms -> 90 s
86400000 ms -> 86400 s
9000000000000 ms -> too long: integer overflow
How It Works
Divide first, convert second.ms / 1000 is done in long, giving 90 for the first value, and toIntExact then turns it into an int.
A full day. 86400000 milliseconds is 86400 seconds, one whole day, which is tiny compared with the int limit.
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.