Java Math.incrementExact() method

21 Mar 2025 | 1 min read

The java.lang.Math.incrementExact() returns the argument incremented by one. It will throw an exception if the result overflows either int or long.

Syntax

Parameter

Return

  • If the argument is Integer.MAX_VALUE or Long.MAX_VALUE, it will throw an ArithmeticException.

Example 1

Output:

 675 

Example 2

Output:

 -52 

Example 3

Output:

 Exception in thread "main" java.lang.ArithmeticException: integer overflow	at java.lang.Math.incrementExact(Math.java:909)	at incrementExactExample3.main(incrementExactExample3.java:7) 

Example 4

Output:

 Exception in thread "main" java.lang.ArithmeticException: long overflow	at java.lang.Math.incrementExact(Math.java:926)	at incrementExactExample4.main(incrementExactExample4.java:7) 
 
Next TopicJava Math