Since Java 8 you can use: Math.toIntExact(long value)
Returns the value of the long argument; throwing an exception if the value overflows an int.
Source code of Math.toIntExact
in JDK 8:
public static int toIntExact(long value) {
if ((int)value != value) {
throw new ArithmeticException("integer overflow");
}
return (int)value;
}