VOOZH about

URL: https://www.geeksforgeeks.org/java/strictmath-subtractexact-in-java-with-examples/

⇱ StrictMath subtractExact() in Java With Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

StrictMath subtractExact() in Java With Examples

Last Updated : 13 Dec, 2021

subtractExact(int num1, int num2)

The subtractExact(int num1, int num2) is an inbuilt method of StrictMath class in java which is used to get the difference of the given arguments num1 and num2. If the result overflows int, it throws an exception. 
Since subtractExact(int num1, int num2) is static, so object creation is not mandatory. 
Syntax :  

public static int subtractExact(int num1, int num2)


Parameters: The method accepts two parameters:  

  • num1: the first integer value and
  • num2: the second integer value to subtract from the first.


Return Value: The method returns the difference of the given arguments num1 and num2
Exception: If the result overflows an int it throws ArithmeticException.
Examples: 

Input: num1 = 750, num2 = 50
Output: 700

Input: num1 = 361, num2 = 929
Output: -568


Below program illustrates the java.lang.StrictMath.subtractExact() method:
Program 1: 


Output: 
Difference of 76761 and 99 = 76662
Difference of 76761 and 786616 = -709855

 

subtractExact(long num1, long num2)

The subtractExact(long num1, long num2) is an inbuilt method of StrictMath class in java which is used to get the difference of the given arguments num1 and num2. If the result overflows long it throws an exception. Since subtractExact(long num1, long num2) is static, so object creation is not mandatory. 
Syntax :  

public static long subtractExact(long num1, long num2)


Parameters: The method accepts two parameters:  

  • num1: the first long value and
  • num2: the second long value to subtract from the first.


Return Value: The method returns the difference of the given arguments num1 and num2
Exception: If the result overflows an long it throws ArithmeticException.
Examples: 

Input: num1 = 750, num2 = 50
Output: 700

Input: num1 = 361, num2 = 929
Output: -568


Below program illustrates the java.lang.StrictMath.subtractExact() method:
Program 1: 


Output: 
Difference of -76342561 and 949 = -76343510
Difference of -76342561 and 78326616 = -154669177

 
Comment