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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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


My Personal Notes arrow_drop_up

lets make it a lil simple

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.