.incrementExact()
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
Contributors
- Anonymous contributor