.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.java
public 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:

22
124
-37

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy

Contributors