Java .toIntExact()
Anonymous contributor
Published Oct 22, 2022
Contribute to Docs
The Math.toIntExact() method returns the integer value of a long type. An exception is thrown if the argument’s value overflows that of an int.
Syntax
Math.toIntExact(long value)
In the function Math.toIntExact(long value), the parameter value is of type long. The function will return an int.
If the value of value is greater than 2147483647L or is less than -2147483648L, then the following exception will be thrown:
Exception in thread "main" java.lang.ArithmeticException: integer overflowat java.base/java.lang.Math.toIntExact
Example
The following example demonstrates the application of .toIntExact() method:
// Check.javapublic class Check {public static void main(String args[]) {long val= 2147483647L;System.out.println(Math.toIntExact(val));}}
This results in the following output:
2147483647
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours