.decrementExact()

The Math.decrementExact() method returns the argument decremented by one. It throws an exception if the result overflows the specified data type of the argument.

Syntax

Math.decrementExact(num)

The num parameter is of type long or int.

Example

The following example uses the .decrementExact() method to decrement the input by 1:

// Main.java
public class Main {
public static void main(String[] args) {
int value = 21;
long input = 123L;
long negValue = -38L;
System.out.println(Math.decrementExact(value));
System.out.println(Math.decrementExact(input));
System.out.println(Math.decrementExact(negValue));
}
}

This will produce the following output:

20
122
-39

Contributors

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

Learn Java on Codecademy

Contributors