Java Tutorial Java Advanced Java References

Java Math - incrementExact() Method



The Java incrementExact() method returns the argument increased by one. The method can be overloaded and it can take int and long arguments. It throws an exception, if the result overflows an int or a long.

Syntax

public static int incrementExact(int x)  
public static long incrementExact(long x)  

Parameters

x Specify the value.

Return Value

Returns the argument increased by one.

Exception

Throws ArithmeticException, if the result overflows an int or a long.

Example:

In the example below, incrementExact() method is used to increase the argument by one.

public class MyClass {
 public static void main(String[] args) {
  int i = 0;
  long j = 145;
  System.out.println(Math.incrementExact(i)); 
  System.out.println(Math.incrementExact(j));    
 }
}

The output of the above code will be:

1
146

❮ Java Math Methods