Java中的 StrictMath SubtractExact() 示例
减法(int num1,int num2)
subtractExact( int num1, int num2 )是Java中 StrictMath 类的内置方法,用于获取给定参数num1和num2的差异。如果结果溢出int ,则会引发异常。
由于 subtractExact( int num1, int num2 ) 是静态的,所以对象创建不是强制性的。
句法 :
public static int subtractExact(int num1, int num2)
参数:该方法接受两个参数:
- num1 : 第一个整数值和
- num2 :从第一个整数值中减去的第二个整数值。
返回值:该方法返回给定参数num1和num2的差值。
异常:如果结果溢出int ,则抛出ArithmeticException 。
例子:
Input: num1 = 750, num2 = 50
Output: 700
Input: num1 = 361, num2 = 929
Output: -568
下面的程序说明了Java.lang.StrictMath.subtractExact() 方法:
方案一:
java
// Java program to illustrate the
// java.lang.StrictMath.subtractExact()
import java.lang.StrictMath;
class Geeks {
// driver code
public static void main(String args[])
{
// Get the int values
// for which the difference is required
int num1 = 76761;
int num2 = 99;
int num3 = 786616;
// Get difference between
// num1 and num2
System.out.println("Difference of " + num1
+ " and " + num2 + " = "
+ StrictMath
.subtractExact(num1, num2));
// Get difference between
// num1 and num3
System.out.println("Difference of " + num1
+ " and " + num3 + " = "
+ StrictMath
.subtractExact(num1, num3));
}
}
java
// Java program to illustrate the
// java.lang.StrictMath.subtractExact()
import java.lang.StrictMath;
class Geeks {
// driver code
public static void main(String args[])
{
// Get the long values
// for which the difference is required
long num1 = -76342561;
long num2 = 949;
long num3 = 78326616;
// Get difference between
// num1 and num2
System.out.println("Difference of " + num1
+ " and " + num2 + " = "
+ StrictMath
.subtractExact(num1, num2));
// Get difference between
// num1 and num3
System.out.println("Difference of " + num1
+ " and " + num3 + " = "
+ StrictMath
.subtractExact(num1, num3));
}
}
输出:
Difference of 76761 and 99 = 76662
Difference of 76761 and 786616 = -709855
减法(long num1,long num2)
subtractExact( long num1, long num2 )是Java中 StrictMath 类的内置方法,用于获取给定参数num1和num2的差异。如果结果长时间溢出,则会引发异常。由于 subtractExact( long num1, long num2 ) 是静态的,所以对象创建不是强制性的。
句法 :
public static long subtractExact(long num1, long num2)
参数:该方法接受两个参数:
- num1 : 第一个 long 值和
- num2 : 从第一个中减去的第二个 long 值。
返回值:该方法返回给定参数num1和num2的差值。
异常:如果结果溢出long它会抛出ArithmeticException 。
例子:
Input: num1 = 750, num2 = 50
Output: 700
Input: num1 = 361, num2 = 929
Output: -568
下面的程序说明了Java.lang.StrictMath.subtractExact() 方法:
方案一:
Java
// Java program to illustrate the
// java.lang.StrictMath.subtractExact()
import java.lang.StrictMath;
class Geeks {
// driver code
public static void main(String args[])
{
// Get the long values
// for which the difference is required
long num1 = -76342561;
long num2 = 949;
long num3 = 78326616;
// Get difference between
// num1 and num2
System.out.println("Difference of " + num1
+ " and " + num2 + " = "
+ StrictMath
.subtractExact(num1, num2));
// Get difference between
// num1 and num3
System.out.println("Difference of " + num1
+ " and " + num3 + " = "
+ StrictMath
.subtractExact(num1, num3));
}
}
输出:
Difference of -76342561 and 949 = -76343510
Difference of -76342561 and 78326616 = -154669177