StrictMath subtractExact() in Java With Examples
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:
// Java praogram to illustrate the // java.lang.StrictMath.subtractExact() import java.lang.StrictMath; class Geeks { // driver code public static void main(String args[]) { // Get the int values // for which the difference is required int num1 = 76761; int num2 = 99; int num3 = 786616; // Get difference between // num1 and num2 System.out.println("Difference of " + num1 + " and " + num2 + " = " + StrictMath .subtractExact(num1, num2)); // Get difference between // num1 and num3 System.out.println("Difference of " + num1 + " and " + num3 + " = " + StrictMath .subtractExact(num1, num3)); } } |
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:
// Java praogram to illustrate the // java.lang.StrictMath.subtractExact() import java.lang.StrictMath; class Geeks { // driver code public static void main(String args[]) { // Get the long values // for which the difference is required long num1 = -76342561; long num2 = 949; long num3 = 78326616; // Get difference between // num1 and num2 System.out.println("Difference of " + num1 + " and " + num2 + " = " + StrictMath .subtractExact(num1, num2)); // Get difference between // num1 and num3 System.out.println("Difference of " + num1 + " and " + num3 + " = " + StrictMath .subtractExact(num1, num3)); } } |
Difference of -76342561 and 949 = -76343510 Difference of -76342561 and 78326616 = -154669177
Recommended Posts:
- StrictMath max() Method in Java With Examples
- StrictMath fma() method in Java with Examples
- StrictMath cos() Method in Java with Examples
- StrictMath min() Method in Java with Examples
- StrictMath ulp() Method In Java with Examples
- StrictMath ceil() Method in Java with Examples
- StrictMath expm1() Method in Java With Examples
- StrictMath multiplyFull() method in Java with Examples
- StrictMath asin() Method in Java With Examples
- StrictMath toIntExact() Method in Java with Examples
- StrictMath multiplyHigh() method in Java with Examples
- Java Math subtractExact(int a , int b) method
- Java Math subtractExact(long x, long y) method
- StrictMath log() Method In Java
- StrictMath pow() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



