Facebook Page Twitter Page LinkedIn Page
× Java Tutorial Java Advanced Java Resources


The Java Math toIntExact() method returns the value of the long argument. The method throws an exception if the value of the argument overflows an int.

Syntax

public static int toIntExact(long value)

Parameters

value Specify the long value.

Return Value

Returns the value of the long argument. Throws an exception if the value overflows an int.

Exception

throws ArithmeticException, if the argument overflows an int.

Example:

In the below example, toIntExact() method returns the value of the long argument.

public class MyClass {
 public static void main(String[] args) {
  System.out.println(Math.toIntExact(555)); 
  System.out.println(Math.toIntExact(Long.MAX_VALUE));   
 }
}

The output of the above code will be:

555

Exception in thread "main" java.lang.ArithmeticException: integer overflow

❮ Java Math Methods