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
Call
What 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
Parameter
Description
a
The value to increase by one.
Return Value
Return Value
Description
int / long
a + 1, when it fits in the type.
Throws ArithmeticException
Only when a is Integer.MAX_VALUE (integer overflow) or Long.MAX_VALUE (long overflow).
Compare with Similar Methods
Different ways to add one:
Method
How it differs
a++ / a + 1
No check: Integer.MAX_VALUE + 1 wraps to -2147483648.
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));
}
}
Output
42
0
10000000000
9223372036854775807
How It Works
Plain increments. 41 becomes 42, and -1 becomes 0; the sign does not matter.
Past the int limit, still a long. 9999999999 is a long literal, so incrementExact returns 10000000000 without trouble.
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);
}
}
Output
plain ++: -2147483648
incrementExact: integer overflow
safe is still: 2147483647
How It Works
The silent restart. After plain++ the counter has jumped from the top of the range to the bottom, -2147483648, and nothing tells the program.
The refusal.incrementExact throws integer overflow before the assignment happens, so the catch block runs.
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);
}
}
Output
visits = 2147483646
visits = 2147483647
counter is full: integer overflow
last good value: 2147483647
How It Works
Starting near the top.Integer.MAX_VALUE - 2 is 2147483645, so the first visit() reaches 2147483646 and the second reaches 2147483647.
The third call fails. 2147483647 is the largest int, so incrementExact throws inside visit() before its println runs.
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.