Java数学加法器(长x,长y)方法

这个 JAVA朗,数学。addExact() 是java中的内置数学函数,返回其参数之和。 如果结果溢出了很长一段时间,它会抛出一个异常。就像addExact(长x,长y)一样 静止的 ,因此对象创建 这不是必需的。

null

语法:

public static long addExact(long x, long y)
Parameter :
 a : the first value
 b : the second value
Return :
This method returns the the sum of its arguments.
Exception :
It throws ArithmeticException - if the result overflows a long

例子: 表示工作 JAVA朗,数学。addExact() 方法

// Java program to demonstrate working
// of java.lang.Math.addExact() method
import java.lang.Math;
class Gfg1 {
// driver code
public static void main(String args[])
{
long x = 9999999999l;
long y = 1l;
System.out.println(Math.addExact(x, y));
}
}


输出:

10000000000

// Java program to demonstrate working
// of java.lang.Math.addExact() method
import java.lang.Math;
class Gfg2 {
// driver code
public static void main(String args[])
{
long a = Long.MAX_VALUE;
long b = 2l;
System.out.println(Math.addExact(a, b));
}
}


输出:

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