Java Math incrementExact() Method

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

Definition

In Java, Math.incrementExact(a) returns a + 1, and throws an ArithmeticException if a is already the largest value of its type. The ++ operator would simply wrap around from Integer.MAX_VALUE to a large negative number.

Use it for counters, ticket numbers, and ID generators that must never silently start over or go negative.

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.incrementExact(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass an int or a long. The method does not change your variable; assign the result back yourself.

Java
int next = Math.incrementExact(41);              // 42
count = Math.incrementExact(count);              // safe count++
Math.incrementExact(Integer.MAX_VALUE);          // throws ArithmeticException
CallWhat it does
Math.incrementExact(41)int overload: returns 42.
Math.incrementExact(99L)long overload: returns 100L.
count = Math.incrementExact(count)A safe replacement for count++.
Math.incrementExact(Integer.MAX_VALUE)Throws ArithmeticException ("integer overflow").

incrementExact(a) does the same job as addExact(a, 1); the dedicated method just says what you mean. Its mirror image is decrementExact.

Library declarations
Java
// java.lang.Math
public static int incrementExact(int a)
public static long incrementExact(long a)

Parameters

ParameterDescription
aThe value to increase by one.

Return Value

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

Compare with Similar Methods

Different ways to add one:

MethodHow it differs
a++ / a + 1No check: Integer.MAX_VALUE + 1 wraps to -2147483648.
decrementExact(a)The overflow-safe a - 1.
addExact(a, 1)Same behaviour, longer to write.
AtomicInteger.incrementAndGet()Thread-safe counter, but it still wraps around silently.

Java Examples

Example 1 — Add One to int and long Values

Ordinary increments, plus a long that is only one step below its maximum.

Java
public class MathIncrementExactBasics {
    public static void main(String[] args) {
        System.out.println(Math.incrementExact(41));
        System.out.println(Math.incrementExact(-1));
        System.out.println(Math.incrementExact(9_999_999_999L));
        System.out.println(Math.incrementExact(Long.MAX_VALUE - 1));
    }
}

How It Works

  1. Plain increments. 41 becomes 42, and -1 becomes 0; the sign does not matter.
  2. Past the int limit, still a long. 9999999999 is a long literal, so incrementExact returns 10000000000 without trouble.
  3. Right at the edge. Long.MAX_VALUE - 1 plus 1 is exactly Long.MAX_VALUE, 9223372036854775807, so this call still succeeds; only the next increment would fail.

Example 2 — ++ Wraps, incrementExact Refuses

Increase the largest int by one with both approaches.

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

        int safe = Integer.MAX_VALUE;
        try {
            safe = Math.incrementExact(safe);
        } catch (ArithmeticException e) {
            System.out.println("incrementExact: " + e.getMessage());
        }
        System.out.println("safe is still:  " + safe);
    }
}

How It Works

  1. The silent restart. After plain++ the counter has jumped from the top of the range to the bottom, -2147483648, and nothing tells the program.
  2. The refusal. incrementExact throws integer overflow before the assignment happens, so the catch block runs.
  3. No half-done update. Because the assignment never completed, safe keeps its old value, 2147483647.

Example 3 — A Visit Counter That Cannot Silently Wrap

A counter that starts two steps from the limit. The third visit is rejected with a clear message.

Java
public class MathIncrementExactCounter {
    private static int visits = Integer.MAX_VALUE - 2;

    static void visit() {
        visits = Math.incrementExact(visits);
        System.out.println("visits = " + visits);
    }

    public static void main(String[] args) {
        try {
            visit();
            visit();
            visit();
        } catch (ArithmeticException e) {
            System.out.println("counter is full: " + e.getMessage());
        }
        System.out.println("last good value: " + visits);
    }
}

How It Works

  1. Starting near the top. Integer.MAX_VALUE - 2 is 2147483645, so the first visit() reaches 2147483646 and the second reaches 2147483647.
  2. The third call fails. 2147483647 is the largest int, so incrementExact throws inside visit() before its println runs.
  3. The exception travels up. It leaves visit(), is caught in main, and the static field still holds the last good value.

Frequently Asked Questions

It returns the argument plus one for an int or long, and throws ArithmeticException if the argument is already Integer.MAX_VALUE or Long.MAX_VALUE.
It wraps around silently to Integer.MIN_VALUE, which is -2147483648. Use count = Math.incrementExact(count) to get an exception instead.
No. It returns the new value and leaves the argument untouched. Assign the result back, as in count = Math.incrementExact(count).
Yes, they behave the same, including the exception messages. incrementExact is just shorter and states the intention.
Java 8, together with decrementExact, addExact, subtractExact, multiplyExact, negateExact, and toIntExact.

Next: log()

Natural logarithm (base e) of a double.

log() 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.

8 people found this page helpful