The java.lang.StrictMath.toIntExact() is an inbuilt method in Java is used to return the value of the long argument. If the result overflows an int it will throw an exception. Object creation is not mandatory as toIntExact(long num) is static.
public static int toIntExact(long num)
Parameters: The method accepts one parameter num of long type whose int value is returned.
Return Value: The method returns the argument as an int.
Exception: If the argument overflows an int it throws ArithmeticException.
Input: num = 2727l Output: 2727 Input: num = -86262l Output: -86262
Below programs illustrate the java.lang.StrictMath.toIntExact() method:
IntExact value of 266526 = 266526
IntExact value of -7226526 = -7226526
Program 3: To demonstrate ArithmeticException
Trying to get IntExact value of: 654456645546 Exception throwm: java.lang.ArithmeticException: integer overflow
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- StrictMath fma() method in Java with Examples
- StrictMath cos() Method in Java with Examples
- StrictMath max() Method in Java With Examples
- StrictMath ulp() Method In Java with Examples
- StrictMath min() Method in Java with Examples
- Java Math toIntExact(long value) Method
- StrictMath ceil() Method in Java with Examples
- StrictMath asin() Method in Java With Examples
- StrictMath expm1() Method in Java With Examples
- StrictMath multiplyFull() method in Java with Examples
- StrictMath multiplyHigh() method in Java with Examples
- StrictMath subtractExact() in Java With Examples
- StrictMath exp() Method in Java
- StrictMath sin() Method in Java
- StrictMath log() Method In Java
- StrictMath pow() Method in Java
- StrictMath tan() Method in Java
- StrictMath cosh() Method in Java
- StrictMath nextUp() Method in Java
- StrictMath getExponent() Method In Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.