Home » Java Math.toIntExact() Method with Examples

Java Math.toIntExact() Method with Examples

by Online Tutorials Library

Java Math.toIntExact() method

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

Test it Now

Output:

230  

Example 2

Test it Now

Output:

-829  

Example 3

Test it Now

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

Test it Now

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

You may also like