Java Math incrementExact() Method
Example
Increment different numbers:
System.out.println(Math.incrementExact(12002));
System.out.println(Math.incrementExact(18));
System.out.println(Math.incrementExact(32));
System.out.println(Math.incrementExact(947));
Try it Yourself »
Definition and Usage
The incrementExact()
method returns 1 more than a specified integer and throws an exception if an overflow occurs. This prevents incorrect results that can occur from the overflow.
Syntax
One of the following:
public static int incrementExact(int x)
public static long incrementExact(long x)
Parameter Values
Parameter | Description |
---|---|
x | Required. An integer to increment from. |
Technical Details
Returns: | An int or long value representing 1 more than an integer. |
---|---|
Throws: | ArithmeticException - If the increment causes an overflow. |
Java version: | 1.8+ |
❮ Math Methods