.decrementExact()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 23, 2022
Contribute to Docs

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

All contributors

Contribute to Docs

Learn Java on Codecademy