Java Math subtractExact(long x, long y) method



We will discuss the Java Math subtractExact(long x, long y) method in Java and understand its functionalities and working.

The subtractExact()is an inbuilt function in the Java Math library. The function returns the difference between the two parameters passed as arguments in the function. The function returns an exception when the returned value overflows the range of values of a particular data type.

It is the most commonly used class for mathematical operations is the java.lang.Math class is a part of the Java Standard Library and is included in the java.lang package.

Syntax

Following is the syntax of the subtractExact() function ?

long subtractExact(long a, long b);

Parameters

In the following method, we have two parameters ?

  • a: The first value to be subtracted from.
  • b: The second value to be subtracted.

Return Value

This method returns the result of the subtraction of a and b i.e (a-b).

In this article, we will see the implementation of the subtractExact() method in the Java code and how it returns the value. We will be writing a Java code where two numbers will be given as an input, the input will be of long data type and we need to find the difference of the two numbers using the function subtractExact() provided in the Java Math library.

Let's understand the problem with the below Example Scenarios ?

Example Scenario 1

Input: a=30 , b=10
Output: 20

Explanation: Here the input numbers given are 30 and 10. We need to find the difference of the two numbers i.e. a?b using the subtractExact() function. Hence the output will be 20 which is equal to a?b.

Example Scenario 2

Input: a= -2  , b=-3
Output: 1

Explanation: The input numbers are ?2 and ?3. Using the subtractExact(a,b) and passing these two numbers as parameters in the function, the function will return the value equal to a?b i.e. ?2?(?3)=?2+3=1. Hence, 1 is the required output.

Let's understand the use of the subtractExact() function in the Java code to see the implementation of the function.

Java Code to see the implementation of subtractExact()

Following are the steps to implement the subtractExact() method in our Java code as below ?

  • We will import the Java Math library using java.lang.Math.

  • After importing the Math library, we can use all the functions in the library including the subtractExact() function.

  • Initialise two variables of long data type.

  • Now we will create another variable to store the value returned by the function.

  • Using subtractExact() function, we will store the value in the variable. The function subtract the second parameter passed in the function from the first parameter.

  • Print the output of the function which will be equal to the difference of first parameter and second parameter.

Example

In this program, we demonstrate the use of the subtractExact() method from the Math class to subtract one long value from another. It initialize two variables a and b, and compute their differences using subtractExact().

import java.lang.Math; 

public class Demo
{
   public static void main(String[] args) {
   	  long a;
   	  long b;
   	  
   	  a=56240389;
   	  b=846270;
   	  
   	  long ans;
   	  ans=Math.subtractExact(a,b); 
   	  
   	  System.out.println("The value returned by the function : "+ans);
   	  ans=Math.subtractExact(b,a);
   	  System.out.println("The value returned by the function : "+ans);
   }
}

The above program produce the following result ?

Output

The value returned by the function : 55394119
The value returned by the function : -55394119

Time complexity? O(1) , because the function subtractExact() returns the output in constant time.

Space complexity ? O(1) , because we are not using any extra space for the implementation of the function.

Java Program to see the overflow using subtractExact()

Following are the steps to implement the subtractExact() method in our java code as described below ?

  • We will import the Java Math library using java.lang.Math

  • Now create two variables of long data type.

  • We will store the minimum value of the long data type in one of the variables and any value in the second one.

  • Now initialise another variable of long data type to store the value returned by the function. We will pass the minimum value of long data type as a first parameter because the function subtract the second parameter passed in the function from the first one.

  • Subtracting the number from the minimum value of the long data type will cause an overflow in the long data type. Thus, the function will show an exception for the long overflow.

Example

In this program, we initialize two variables a and b with Long.MIN_VALUE and 7 using subtractExact() method. The subtraction a - b causes an overflow, which results in an ArithmeticException being thrown.

import java.lang.Math; 
public class Main
{
   public static void main(String[] args) {
   	  long a;
   	  long b;
   	  
   	  a=Long.MIN_VALUE;
   	  b=7;
   	  
   	  long ans; 
   	  ans=Math.subtractExact(a,b); 
   	  System.out.println("The value returned by the function : "+ans);  
   }
}

Output

The above program produce the following result ?

Exception in thread "main" java.lang.ArithmeticException: long overflow
        at java.base/java.lang.Math.subtractExact(Math.java:887)
        at Main.main(Main.java:19)

Conclusion

The Java Math subtractExact(long x, long y) method and the implementation of the function in Java and its functionalities was discussed in the article. We understand the implementation of subtractExact() function by writing a java code and also try to understand the overflow of value in the function.

I hope you have understood the Java Math subtractExact() method and its implementation in Java after reading this article.

Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-01-28T11:39:30+05:30

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements