Java Math decrementExact() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

Definition

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
CallWhat 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

ParameterDescription
aThe value to decrease by one.

Return Value

Return ValueDescription
int / longa - 1, when it fits in the type.
Throws ArithmeticExceptionOnly when a is Integer.MIN_VALUE (integer overflow) or Long.MIN_VALUE (long overflow).

Compare with Similar Methods

Different ways to subtract one:

MethodHow it differs
a-- / a - 1No check: Integer.MIN_VALUE - 1 wraps to 2147483647.
incrementExact(a)The overflow-safe a + 1.
subtractExact(a, 1)Same behaviour with an explicit amount.
Math.max(0, a - 1)Stops at zero instead of at the type limit.

Java Examples

Example 1 — Subtract One From int and long Values

Normal decrements, including going below zero and a long beyond the int range.

Java
public class MathDecrementExactBasics {
    public static void main(String[] args) {
        System.out.println(Math.decrementExact(10));
        System.out.println(Math.decrementExact(1));
        System.out.println(Math.decrementExact(0));
        System.out.println(Math.decrementExact(-9_000_000_000L));
    }
}

How It Works

  1. Counting down. 10 becomes 9 and 1 becomes 0; decrementExact is simply a - 1 when there is room.
  2. Below zero is allowed. Zero minus one is -1. The method only cares about the limits of the type, not about zero.
  3. A long value. -9000000000 is outside the int range, so it is a long, and the long overload returns -9000000001.

Example 2 — -- Wraps to MAX_VALUE, decrementExact Throws

Decrease the smallest int and the smallest long by one.

Java
public class MathDecrementExactWrap {
    public static void main(String[] args) {
        int plain = Integer.MIN_VALUE;
        plain--;
        System.out.println("plain --:       " + plain);

        try {
            Math.decrementExact(Integer.MIN_VALUE);
        } catch (ArithmeticException e) {
            System.out.println("int threw:      " + e.getMessage());
        }
        try {
            Math.decrementExact(Long.MIN_VALUE);
        } catch (ArithmeticException e) {
            System.out.println("long threw:     " + e.getMessage());
        }
    }
}

How It Works

  1. 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.
  2. An int-sized refusal. decrementExact(Integer.MIN_VALUE) detects the same step and throws integer overflow.
  3. 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());
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: divideExact()

Integer division that throws on the MIN_VALUE / -1 overflow (Java 18+).

divideExact() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful