119
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
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