Java Math toIntExact() Method
Example
Convert long
values to int
:
System.out.println(Math.toIntExact(1500000L));
System.out.println(Math.toIntExact(-32L));
System.out.println(Math.toIntExact(-86345L));
System.out.println(Math.toIntExact(25L));
Try it Yourself »
Definition and Usage
The toIntExact()
method Converts a long
value to an int
and throws an exception if the conversion results in an overflow. This prevents incorrect results that can occur from the overflow.
Syntax
public static int toIntExact(long x)
Parameter Values
Parameter | Description |
---|---|
x | Required. An integer to negate. |
Technical Details
Returns: | An int value representing the contents of a long integer. |
---|---|
Throws: | ArithmeticException - If the conversion causes an overflow. |
Java version: | 1.8+ |
❮ Math Methods