Open In App

StrictMath subtractExact() in Java With Examples

Last Updated : 13 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Java program 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));
    }
}


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: 

java




// Java program 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));
    }
}


Output: 

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

 



Similar Reads

Java Math subtractExact(int a , int b) method
The java.lang.Math.subtractExact() is a built-in math function in java that returns the difference of the arguments. It throws an exception if the result overflows an int. As subtractExact(int a, int b) is static, so object creation is not required. Syntax: public static int subtractExact(int a, int b) Parameters: a: the first valueb: the second va
2 min read
Java Math subtractExact(long x, long y) method
The java.lang.Math.subtractExact() is a built-in math function in java which returns the difference of the arguments.It throws an exception if the result overflows a long.As subtractExact(long x, long y) is static, so object creation is not required. Syntax : public static long subtractExact(long x, long y) Parameter : x : the first value y : the s
1 min read
StrictMath asin() Method in Java With Examples
The java.lang.StrictMath.asin() is an inbuilt method of StrictMath class which is used to return the arc sine of a specified value. The returned angle is in within the range of -pi/2 and pi/2. The method gives rise to two special results: If the absolute value of the argument is greater than 1 or the argument is itself a NaN then the result is also
2 min read
StrictMath ceil() Method in Java with Examples
All methods of java.lang.StrictMath class : Set 1, Set 2The java.lang.StrictMath.ceil() is an inbuilt method in Java is used to return the smallest double value, greater than or equal to the given double argument and equal to an integer. It gives rise to three special results: The result is the same as the argument when the given argument is equal
2 min read
StrictMath cos() Method in Java with Examples
All methods of java.lang.StrictMath class : Set 1, Set 2The java.lang.StrictMath.cos() is an inbuilt method in Java which is used to return the cosine value of a given angle. The method returns NaN when the given argument is NaN or infinity.Syntax: public static double cos(double num) Parameters: The method accepts one parameter num of double type
2 min read
StrictMath expm1() Method in Java With Examples
The java.lang.StrictMath.expm1() is an inbuilt method in Java which is used to return the exponential e^num -1 for a given value of num. The method gives rise to four different cases: The method returns NaN when the given argument is NaN.The result is positive infinity when the argument is positive infinity.The result is negative infinity when the
2 min read
StrictMath min() Method in Java with Examples
The java.lang.StrictMath.min() method returns the smaller of two values. There are four variations of this method with different types of parameters passed. All of them are discussed below : min(double num1, double num2) is the inbuilt method of StrictMath class which is used to get the least of given two double values arguments. It returns NaN whe
6 min read
StrictMath max() Method in Java With Examples
The java.lang.StrictMath.max() method returns the larger of two values. There are four variations of this method with different types of parameters passed. All of them are discussed below : The max(double num1, double num2)is the inbuilt method which is used to get the maximum of two given double values arguments. It returns the same value when num
6 min read
StrictMath ulp() Method In Java with Examples
ulp(double num) The ulp(double num) is an inbuilt method of StrictMath class in java which is used to get the size of an ulp of the given argument. An ulp of a double value is the positive distance between this floating point value and the double value which is next larger in magnitude.Some special cases are: The result is NaN, if the argument is N
3 min read
StrictMath toIntExact() Method in Java with Examples
The java.lang.StrictMath.toIntExact() is an inbuilt method in Java is used to return the value of the long argument. If the result overflows an int it will throw an exception. Object creation is not mandatory as toIntExact(long num) is static. Syntax: public static int toIntExact(long num) Parameters: The method accepts one parameter num of long ty
2 min read
Practice Tags :