Java Math incrementExact(int x) method


We will explore Java Math incrementExact(int x) method by using the function in Java and understanding its different functionalities.

An incrementExact() function is an in-built function in Java in the Math library. This function is used to return a value equal to the parameter passed in the function increased by 1. The function returns an exception due to integer overflow if the value of the integer passed in the function as an argument overflows depends on the data type passed in the function i.e. either int or long.

Syntax

Syntax of the function −

int a;
int incrementExact(a);

long a;
long incrementExact(a);

The parameter passed in the function is just an integer value of either data type, int or long as mentioned. And the return value is the value equal to the parameter passed in the function increased by 1. The function gives an exception if the value of the parameter passed in the function overflows the given datatype.

In this article, we will discuss the implementation of incrementExact() function in Java and how it works. We will write a Java code where we will be given a number as an input and we need to print the value equal to input value increased by 1 using the incrementExact() function.

Let’s understand the problem with the below illustrations.

INPUT : x=6
OUTPUT : 7

Explanation − Here we are given an integer equal to 6. We need to print the value equal to the given integer increased by 1 using the incrementExact() output. Hence, the output will be 7.

INPUT : x= -10
OUTPUT : -9

Explanation − The given integer value is -10. Using the incrementExact() function, passing the x as an argument in the function we will get the value equal to x increased by 1 which will be equal to -9, which is the required output.

Let’s implement the incrementExact() function in our Java code to understand how to implement the function and its working.

Java program to see the functionality of incrementExact() function:

The steps to follow to implement the function in our code are given below.

  • Import the java math library using java.lang.Math.

  • Initialise a variable to store the input in the code.

  • Then we will again create another variable to store the output of the incrementExact() function where the passed parameter will be the input variable.

  • Return the variable where we store the value returned by the function which will be equal to the value of the input variable increased by 1, which is the output we require.

Example

The Java code for the implementation of the function −

//Java Code to see the implementation of the incrementExact() function
import java.lang.Math; //to import the math library

public class Main {
   public static  void main(String [] args) { 
      //to store the input value
      long x;
      x=1578;
        
      //to store the value returned by the incrementExact() function
      long ans;
      ans=Math.incrementExact(x); //passing the parameter as input value
       
      //printing the output of the function
      System.out.println("The output after the implementation of the function : "+ans);
   }
}

Output

The output after the implementation of the function : 1579  

Time Complexity − O(1), as constant time is taken to perform the operation in the function.

Space Complexity − O(1), as no extra space is required.

Java program to see when the output of the function overflows:

The steps to implement the incrementExact() function are given below.

  • Import the math library in Java using java.lang.Math.

  • Take the input value in a variable as the maximum value of integer to see the overflow of the value in the function.

  • Store the output of the function incrementExact() in a variable where the passed parameter in the function is the input value.

  • The code will show an exception for the integer overflow as the parameter passed in the function is the maximum value of the integer which when increased by 1 shows the overflow of value.

Example

The Java code to see the overflow of integer value

//Java Code to see the implementation of the incrementExact() function
import java.lang.Math; //to import the math library

public class Main {
   public static  void main(String [] args) { 
      //to store the input value
      long x;
      x=Long.MAX_VALUE;
        
      //to store the value returned by the incrementExact() function
      long ans;
      ans=Math.incrementExact(x); //passing the parameter as input value
        
      //printing the output of the function
      System.out.println(ans);
   }
}

Output

Exception in thread "main" java.lang.ArithmeticException: integer overflow
   at java.base/java.lang.Math.incrementExact(Math.java:964)
   at Main.main(Main.java:14)

Updated on: 18-Apr-2023

108 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements