java.lang.Math.subtractExact()是java中的內置數學函數,該函數返回參數的差值。如果結果溢出了long則拋出異常.astractExact(long x,long y)是靜態的,因此對象創建是不需要。
用法:
public static long subtractExact(long x, long y) 參數: x:the first value y:the second value to be subtracted from the first 返回: 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)
相關用法
- Java Math addExact(long x, long y)用法及代碼示例
- Java Math toIntExact(long value)用法及代碼示例
- Java LongMath.divide(long, long, RoundingMode)用法及代碼示例
- Java Longs.checkedSubtract(long a, long b)用法及代碼示例
- Java LongMath.checkedAdd(long a, long b)用法及代碼示例
- Java Longs.indexOf(long[] array, long target)用法及代碼示例
- Java Longs.indexOf(long[] array, long[] target)用法及代碼示例
- Java Duration ofSeconds(long, long)用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Guava LongMath mean(long x, long y)用法及代碼示例
注:本文由純淨天空篩選整理自Niraj_Pandey大神的英文原創作品 Java Math subtractExact(long x, long y) method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。