In Java, Math.decrementExact(a) returns a - 1, and throws an ArithmeticException if a is already the smallest value of its type. The -- operator would instead wrap around from Integer.MIN_VALUE to the largest int.
It is the counting-down partner of incrementExact: use it for countdowns, remaining-quota counters, and indexes that walk backwards through data.
A static method of the java.lang.Math class, added in Java 8 (JDK 1.8). Call it on the class name, as in Math.decrementExact(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass an int or a long. Assign the result back, because the argument itself is not changed.
Java
int less = Math.decrementExact(10); // 9
quota = Math.decrementExact(quota); // safe quota--
Math.decrementExact(Integer.MIN_VALUE); // throws ArithmeticException
Call
What it does
Math.decrementExact(10)
int overload: returns 9.
Math.decrementExact(0)
Returns -1: going below zero is fine, only the type limit matters.
Math.decrementExact(-9_000_000_000L)
long overload: returns -9000000001L.
Math.decrementExact(Integer.MIN_VALUE)
Throws ArithmeticException ("integer overflow").
decrementExact(a) behaves like subtractExact(a, 1). It protects only against the type's minimum; if you want a counter to stop at zero, compare it with zero or use Math.max(0, a - 1).
Library declarations
Java
// java.lang.Math
public static int decrementExact(int a)
public static long decrementExact(long a)
Parameters
Parameter
Description
a
The value to decrease by one.
Return Value
Return Value
Description
int / long
a - 1, when it fits in the type.
Throws ArithmeticException
Only when a is Integer.MIN_VALUE (integer overflow) or Long.MIN_VALUE (long overflow).
Compare with Similar Methods
Different ways to subtract one:
Method
How it differs
a-- / a - 1
No check: Integer.MIN_VALUE - 1 wraps to 2147483647.
plain --: 2147483647
int threw: integer overflow
long threw: long overflow
How It Works
Wrap to the other end. One below -2147483648 does not exist, so plain-- jumps to the very top of the range, 2147483647, with no warning.
An int-sized refusal.decrementExact(Integer.MIN_VALUE) detects the same step and throws integer overflow.
The long limit.Long.MIN_VALUE is the end of the road for a long, and the long overload reports it as long overflow.
Example 3 — Walk Backwards Through an Array
Start at the last index and count down with decrementExact, then see what happens at the limit.
Java
public class MathDecrementExactBackwards {
public static void main(String[] args) {
String[] names = {"Asha", "Ben", "Chen"};
for (int i = Math.decrementExact(names.length); i >= 0; i = Math.decrementExact(i)) {
System.out.println("index " + i + ": " + names[i]);
}
try {
Math.decrementExact(Integer.MIN_VALUE);
} catch (ArithmeticException e) {
System.out.println("floor reached: " + e.getMessage());
}
}
}
Output
index 2: Chen
index 1: Ben
index 0: Asha
floor reached: integer overflow
How It Works
Last index first. The array has 3 elements, so decrementExact(names.length) is 2, the index of "Chen", and the loop prints the names in reverse order.
The loop stops normally. After index 0 the update gives -1, which fails the test i >= 0; no overflow is involved, so for such small values i-- would work the same.
The real limit. The extra call shows where decrementExact differs: at Integer.MIN_VALUE it throws, and the catch block prints integer overflow.
Frequently Asked Questions
It returns the argument minus one for an int or long, and throws ArithmeticException if the argument is already Integer.MIN_VALUE or Long.MIN_VALUE.
It wraps around silently to Integer.MAX_VALUE, 2147483647. Use count = Math.decrementExact(count) to get an exception instead.
No. Math.decrementExact(0) returns -1. It only guards the minimum of the type. To stop at zero, use Math.max(0, a - 1) or compare the value first.
Yes, they behave the same, including the exception messages. decrementExact is shorter and clearer.
Java 8, together with incrementExact, addExact, subtractExact, multiplyExact, negateExact, and toIntExact.