Java.lang Package Classes

Java Math - incrementExact() Method



The java.lang.Math.incrementExact() method returns the argument increased by one. It throws an exception, if the result overflows a long.

Syntax

 
public static long incrementExact(long x)  

Parameters

x Specify the value to increment.

Return Value

Returns the argument increased by one.

Exception

Throws ArithmeticException, if the result overflows a long.

Example:

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

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  long 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.lang - Math

5