Java的.lang.Math.subtractExact()是一个内置在Java数学函数返回的差
参数。如果结果长时间溢出,则会引发异常.astractExact(long x,long y)是静态的,
因此不需要创建对象。
句法 :
public static long subtractExact(long x, long y)
Parameter :
x : the first value
y : the second value to be subtracted from the first
Return :
This method returns the difference of the arguments.
Exception :
It throws ArithmeticException - if the result overflows a long.
示例:显示Java.lang.Math.subtractExact()方法的工作。
// Java program to demonstrate working
// of java.lang.Math.subtractExact() method
import java.lang.Math;
class Gfg1 {
// driver code
public static void main(String args[])
{
long x = 11111111111l;
long y = 999l;
System.out.println(Math.subtractExact(x, y));
}
}
输出:
11111110112
// Java program to demonstrate working
// of java.lang.Math.subtractExact() method
import java.lang.Math;
class Gfg2 {
// driver code
public static void main(String args[])
{
long a = Long.MIN_VALUE;
long b = 1;
System.out.println(Math.subtractExact(a, b));
}
}
输出:
Runtime Error :
Exception in thread "main" java.lang.ArithmeticException: long overflow
at java.lang.Math.subtractExact(Math.java:849)
at Gfg2.main(File.java:13)