Java数学精确减法(int a,int b)方法

这个 JAVA朗,数学。减法 是java中的一个内置数学函数,返回 论据。如果结果溢出了一个int,它会抛出一个异常 静止的 所以 不需要创建对象。

null

语法:

public static int subtractExact(int a, int b)
Parameter :
 a : the first value
 b : 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 an int

例子: 表示工作 JAVA朗,数学。减法 方法

// 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[])
{
int a = 300 ;
int b = 200 ;
System.out.println(Math.subtractExact(a, b));
}
}


输出:

100

// 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[])
{
int x = Integer.MIN_VALUE;
int y = 10 ;
System.out.println(Math.subtractExact(x, y));
}
}


输出:

Runtime Error :
Exception in thread "main" java.lang.ArithmeticException: integer overflow
    at java.lang.Math.subtractExact(Math.java:829)
    at Gfg2.main(File.java:13)
© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享