# 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:

## 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```

Article Tags :
Practice Tags :