Java.lang Package Classes

Java StrictMath - subtractExact() Method



The java.lang.StrictMath.subtractExact() method returns difference of its arguments. It throws an exception, if the result overflows a long.

Syntax

 
public static long subtractExact(long x, long y)  

Parameters

x Specify the first value.
y Specify the second value to subtract from first.

Return Value

Returns difference of its arguments.

Exception

Throws ArithmeticException, if the result overflows a long.

Example:

In the example below, subtractExact() method is used to find difference of given numbers.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  long x = 12;
  long y = 17;

  long p = 145;
  long q = 139;
  
  System.out.println(StrictMath.subtractExact(x, y)); 
  System.out.println(StrictMath.subtractExact(p, q));    
 }
}

The output of the above code will be:

-5
6

❮ Java.lang - StrictMath