Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int.

Syntax:

public static int toIntExact (long value)

Parameters:

The parameter ‘value’ represents the long value.

Return Value:

The toIntExact () method returns an int value.

Throws:

The toIntExact () method throws:

ArithmeticException- if the argument overflows an int

 Example 1:

Output:

Example 2:

Output:

Example 3:

Output:

Example 4:

Output: