Java Math.toIntExact() method

21 Mar 2025 | 1 min read

The java.lang.Math.toIntExact() returns the value of long argument. It will throw an exception if the result overflows either int or long.

Syntax

Parameter

Return

  • If the argument is Long.MAX_VALUE or Long.MIN_VALUE, it will throw an ArithmeticException.

Example 1

Output:

 230 

Example 2

Output:

 -829 

Example 3

Output:

 Exception in thread "main" java.lang.ArithmeticException: integer overflow	at java.lang.Math.toIntExact(Math.java:1011)	at toIntExactExample3.main(toIntExactExample3.java:6) 

Example 4

Output:

 Exception in thread "main" java.lang.ArithmeticException: integer overflow	at java.lang.Math.toIntExact(Math.java:1011)	at toIntExactExample4.main(toIntExactExample4.java:6) 
 
Next TopicJava Math