Java Math.incrementExact() method

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

Test it Now

Output:

675

Example 2

Test it Now

Output:

-52

Example 3

Test it Now

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

Test it Now

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




Latest Courses