Java .incrementExact()
Anonymous contributor
Published Oct 26, 2022
The Math.incrementExact() method returns the argument incremented by one. It throws an exception if the result overflows the specified data type (long or int).
Syntax
Math.incrementExact(num) The num parameter is of type long or int.
Example
The following example uses the .incrementExact() method to increase the input by one:
// Main.javapublic class Main {public static void main(String[] args) {int value = 21;long input = 123L;long negValue = -38L;System.out.println(Math.incrementExact(value));System.out.println(Math.incrementExact(input));System.out.println(Math.incrementExact(negValue));}}
This will produce the following output:
22124-37
All contributors
- Anonymous contributor
Learn Java on Codecademy
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours