# StrictMath subtractExact() in Java With Examples

In java, the subtractExact() is a static method of the class StrictMath. It is available in ‘java.lang’ package.

In this article, we will discuss StrictMath and some of its inbuilt methods. We will also see the implementation of subtractExact() method and how it is different from other methods of this class.

## StrictMath Class in Java

StrictMath is a final class that extends object class. We can use its methods without creating instance because all the methods of this class are static and we can call a static method without an object.

### To call a Static Mehtod

Class_name.static_method_name


### To import StrictMath Class

import java.lang.StrictMath;


Let’s discuss few methods of StrictMath class first after that we come to its subtractExact() method in the next section.

• abs( value ) − returns the positive value of the given argument. It accepts one argument only.

• ceil( value ) − It takes double value as an argument and returns a rounded value greater than the given argument.

• floor( value ) − It takes double value as an argument and returns a rounded value smaller than the given argument.

• log( value ) − It takes double value and returns the log value to the base e.

• max(value1, value2) − returns the maximum of the given two arguments.

• min(value1, value2) − returns the minimum of the given two arguments.

• random( value ) − It generates a random number between the range from 0 to 1.

• pow(value1, value2) − It takes two argument and returns the value1 to the power value2.

• round( value ) − It returns the closest integer value of the given argument.

### Example

In this example, we will implement the above discussed methods for our better understanding. We use the class name to call all these methods.

import java.lang.StrictMath;
public class Methods {
public static void main(String[] args) {
int n1 = 45;
int n2 = 9;
double d1 = 46.992;
double d2 = 34.27;
System.out.println("Printing a random value between 0 and 1: " + StrictMath.random());
System.out.println("Ceil value of d2: " + StrictMath.ceil(d2));
System.out.println("Absolute value of d1: " + StrictMath.abs(d1));
System.out.println("Floor value of d2: " + StrictMath.floor(d2));
System.out.println("Floor modulus value of n1 and n2: " + StrictMath.floorMod(n1, n2));
System.out.println("Logarithmic value of d2: " + StrictMath.log(d2));
System.out.println("Maximum value between n1 and n2: " + StrictMath.max(n1, n2));
System.out.println("Minimum value between n1 and n2: " + StrictMath.min(n1, n2));
System.out.println(" 9 to power 2 is: " + StrictMath.pow(n2, 2));
System.out.println("Rounded value of d1: " + StrictMath.round(d1));
}
}


### Output

Printing a random value between 0 and 1: 0.5155915867224573
Ceil value of d2: 35.0
Absolute value of d1: 46.992
Floor value of d2: 34.0
Floor modulus value of n1 and n2: 0
Logarithmic value of d2: 3.5342703358865175
Maximum value between n1 and n2: 45
Minimum value between n1 and n2: 9
9 to power 2 is: 81.0
Rounded value of d1: 47


## subtractExact() Method

The subtractExact() method calculates the difference between two given arguments and returns it. It works with integer and long primitive datatypes.

All the methods we have discussed so far do not throw any kind of exception. But, It throws the ArithmeticException when the result exceeds the range of type of its arguments.

### Syntax

StrictMath.strictExact(val1, val2);


It will subtract the ‘val2’ from ‘val1’.

### Example 1

The following example illustrates the implementation of subtractExact() method with integer datatype.

import java.lang.StrictMath;
public class Methods {
public static void main(String[] args) {
int i1 = 45;
int i2 = 9;
System.out.println("Difference between i1 and i2: " + StrictMath.subtractExact(i1, i2));
}
}


### Output

Difference between i1 and i2: 36


### Example 2

In this example, we will see it working with long datatypes.

import java.lang.StrictMath;
public class Methods {
public static void main(String[] args) {
long l1 = 459653499;
long l2 = 287933475;
System.out.println("Difference between l1 and l2: " + StrictMath.subtractExact(l1, l2));
}
}


### Output

Difference between l1 and l2: 171720024


## Conclusion

The class StrictMath is very useful when we need to do mathematical calculations. It provides a variety of inbuilt methods to perform operations on numerical datatypes. In this article, we have understood StrictMath class and its inbuilt method subtractExact().

Updated on: 05-May-2023

56 Views 