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 an int.

Syntax

public static int incrementExact(int x)   

Parameters

x Specify the value to increment.

Return Value

Returns the argument increased by one.

Exception

Throws ArithmeticException, if the result overflows an int.

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) {
  int i = 0;
  int 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