JDK 15中的确切绝对整数
JDK 15 Early Access Build b18向Math和StrictMath類引入了新方法,這些方法將在提供的值超出方法所支持的范圍時拋出ArithmeticException ,而不會發生溢出。 這些方法為Java中的“絕對值”概念帶來了Math.addExact , Math.subtractExact和Math.multiplyExact之類的方法帶來的基本算術功能。
在JDK 15之前, Integer.MIN_VALUE和Long.MIN_VALUE使相應的方法Math.abs和StrictMath.abs返回相同的負數,如MIN_VALUE可能的最大負值所表示。 此行為在Javadoc文檔中針對受影響的方法進行了描述,并通過下面顯示的代碼進行了演示( 可在GitHub上找到 ):
演示JDK之前的15種絕對值方法
/** * Demonstrates "absExact" methods added to {@link Math} * and {@link StrictMath} with JDK 15 Early Access Build b18 * (JDK-8241374: https://bugs.openjdk.java.net/browse/JDK-8241374 ). */ public class AbsoluteExactness { public void demonstrateMathAbsInteger( final int integer) { out.println( "Math.abs(" + integer + "): " + Math.abs(integer)); } public void longNumber) demonstrateMathAbsLong( final long longNumber) { out.println( "Math.abs(" + longNumber + "L): " + Math.abs(longNumber)); } public void demonstrateStrictMathAbsInteger( final int integer) { out.println( "StrictMath.abs(" + integer + "): " + StrictMath.abs(integer)); } public void longNumber) demonstrateStrictMathAbsLong( final long longNumber) { out.println( "StrictMath.abs(" + longNumber + "L): " + StrictMath.abs(longNumber)); } public static void main( final String[] arguments) { final AbsoluteExactness instance = new AbsoluteExactness(); // Demonstrate pre-JDK 15 Math/StrictMath "abs" functions on minimum values. instance.demonstrateMathAbsInteger(Integer.MIN_VALUE+ 1 ); instance.demonstrateMathAbsInteger(Integer.MIN_VALUE); instance.demonstrateMathAbsLong(Long.MIN_VALUE+ 1 ); instance.demonstrateMathAbsLong(Long.MIN_VALUE); instance.demonstrateStrictMathAbsInteger(Integer.MIN_VALUE+ 1 ); instance.demonstrateStrictMathAbsInteger(Integer.MIN_VALUE); instance.demonstrateStrictMathAbsLong(Long.MIN_VALUE+ 1 ); instance.demonstrateStrictMathAbsLong(Long.MIN_VALUE); } }執行上述代碼后,將輸出以下輸出:
Math.abs(- 2147483647 ): 2147483647 Math.abs(- 2147483648 ): - 2147483648 Math.abs(-9223372036854775807L): 9223372036854775807 Math.abs(-9223372036854775808L): - 9223372036854775808 StrictMath.abs(- 2147483647 ): 2147483647 StrictMath.abs(- 2147483648 ): - 2147483648 StrictMath.abs(-9223372036854775807L): 9223372036854775807 StrictMath.abs(-9223372036854775808L): - 9223372036854775808此輸出表明int和long范圍內的最大負允許值導致從Math和StrictMath上的適當abs方法返回相同的值。
JDK 15 Early Access Build b18引入了absExact方法,該方法在這種情況下拋出ArithmeticException而不是返回負值。 以下代碼( 在GitHub上也有 )證明了這一點:
演示JDK 15引入的絕對方法
public class AbsoluteExactness { public void demonstrateMathAbsExactInteger( final int integer) { try { out.println( "Math.absExact(" + integer + "): " + Math.absExact(integer)); } catch (ArithmeticException exception) { err.println( "Math.absExact(" + integer + "): " + exception); } } public void longNumber) demonstrateMathAbsExactLong( final long longNumber) { try { out.println( "Math.absExact(" + longNumber + "L): " + Math.absExact(longNumber)); } catch (ArithmeticException exception) { err.println( "Math.absExact(" + longNumber + "L): " + exception); } } public void demonstrateStrictMathAbsExactInteger( final int integer) { try { out.println( "StrictMath.absExact(" + integer + "): " + StrictMath.absExact(integer)); } catch (ArithmeticException exception) { err.println( "StrictMath.absExact(" + integer + "):" + exception); } } public void longNumber) demonstrateStrictMathAbsExactLong( final long longNumber) { try { out.println( "StrictMath.absExact(" + longNumber + "L): " + StrictMath.absExact(longNumber)); } catch (ArithmeticException exception) { err.println( "StrictMath.absExact(" + longNumber + "L): " + exception); } } public static void main( final String[] arguments) { final AbsoluteExactness instance = new AbsoluteExactness(); // Demonstrate JDK 15-introduced Math/StrictMath "absExact" functions // on minimum values. instance.demonstrateMathAbsExactInteger(Integer.MIN_VALUE+ 1 ); instance.demonstrateMathAbsExactInteger(Integer.MIN_VALUE); instance.demonstrateMathAbsExactLong(Long.MIN_VALUE+ 1 ); instance.demonstrateMathAbsExactLong(Long.MIN_VALUE); instance.demonstrateStrictMathAbsExactInteger(Integer.MIN_VALUE+ 1 ); instance.demonstrateStrictMathAbsExactInteger(Integer.MIN_VALUE); instance.demonstrateStrictMathAbsExactLong(Long.MIN_VALUE+ 1 ); instance.demonstrateStrictMathAbsExactLong(Long.MIN_VALUE); }接下來顯示此代碼的輸出,并演示將MIN_VALUE傳遞給absExact方法時引發的清除異常消息。
Math.absExact(- 2147483647 ): 2147483647 Math.absExact(- 2147483648 ): java.lang.ArithmeticException: Overflow to represent absolute value of Integer.MIN_VALUE Math.absExact(-9223372036854775807L): 9223372036854775807 Math.absExact(-9223372036854775808L): java.lang.ArithmeticException: Overflow to represent absolute value of Long.MIN_VALUE StrictMath.absExact(- 2147483647 ): 2147483647 StrictMath.absExact(- 2147483648 ):java.lang.ArithmeticException: Overflow to represent absolute value of Integer.MIN_VALUE StrictMath.absExact(-9223372036854775807L): 9223372036854775807 StrictMath.absExact(-9223372036854775808L): java.lang.ArithmeticException: Overflow to represent absolute value of Long.MIN_VALUE我發現,對于意外的極端情況拋出異常通常要比返回“某物”更好,這需要我閱讀Javadoc來了解該情況是什么以及在該情況下返回了什么。 該異常使我們很明顯地遇到了邊緣情況,而不是發現從絕對值函數調用返回的負數僅在某個時間以后才實現,并且在代碼中“下游”。 如果沒有其他要求,那么僅使用Math.absExact和StrictMath.absExact方法就可以向Java開發人員暗示,在使用Java的數學庫來計算絕對值時要考慮一些“非精確”的可能性,并且這種實現可能導致閱讀Javadoc找出那些不確切的情況。
翻譯自: https://www.javacodegeeks.com/2020/05/exact-absolute-integral-numbers-in-jdk-15.html
總結
以上是生活随笔為你收集整理的JDK 15中的确切绝对整数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linuxu盘挂载的命令(linux u
- 下一篇: 易语言实现ddos攻击(易语言制作ddo