JDK 15中的确切绝对整数
JDK 15 Early Access Build b18向Math和StrictMath類引入了新方法,這些方法將在提供的值超出方法所支持的范圍時(shí)拋出ArithmeticException ,而不會(huì)發(fā)生溢出。 這些方法為Java中的“絕對(duì)值”概念帶來(lái)了Math.addExact , Math.subtractExact和Math.multiplyExact之類的方法帶來(lái)的基本算術(shù)功能。
在JDK 15之前, Integer.MIN_VALUE和Long.MIN_VALUE使相應(yīng)的方法Math.abs和StrictMath.abs返回相同的負(fù)數(shù),如MIN_VALUE可能的最大負(fù)值所表示。 此行為在Javadoc文檔中針對(duì)受影響的方法進(jìn)行了描述,并通過(guò)下面顯示的代碼進(jìn)行了演示( 可在GitHub上找到 ):
演示JDK之前的15種絕對(duì)值方法
/** * 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); } }執(zhí)行上述代碼后,將輸出以下輸出:
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范圍內(nèi)的最大負(fù)允許值導(dǎo)致從Math和StrictMath上的適當(dāng)abs方法返回相同的值。
JDK 15 Early Access Build b18引入了absExact方法,該方法在這種情況下拋出ArithmeticException而不是返回負(fù)值。 以下代碼( 在GitHub上也有 )證明了這一點(diǎn):
演示JDK 15引入的絕對(duì)方法
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); }接下來(lái)顯示此代碼的輸出,并演示將MIN_VALUE傳遞給absExact方法時(shí)引發(fā)的清除異常消息。
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我發(fā)現(xiàn),對(duì)于意外的極端情況拋出異常通常要比返回“某物”更好,這需要我閱讀Javadoc來(lái)了解該情況是什么以及在該情況下返回了什么。 該異常使我們很明顯地遇到了邊緣情況,而不是發(fā)現(xiàn)從絕對(duì)值函數(shù)調(diào)用返回的負(fù)數(shù)僅在某個(gè)時(shí)間以后才實(shí)現(xiàn),并且在代碼中“下游”。 如果沒(méi)有其他要求,那么僅使用Math.absExact和StrictMath.absExact方法就可以向Java開(kāi)發(fā)人員暗示,在使用Java的數(shù)學(xué)庫(kù)來(lái)計(jì)算絕對(duì)值時(shí)要考慮一些“非精確”的可能性,并且這種實(shí)現(xiàn)可能導(dǎo)致閱讀Javadoc找出那些不確切的情況。
翻譯自: https://www.javacodegeeks.com/2020/05/exact-absolute-integral-numbers-in-jdk-15.html
總結(jié)
以上是生活随笔為你收集整理的JDK 15中的确切绝对整数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linuxu盘挂载的命令(linux u
- 下一篇: 医疗保健数据接口_应用的大数据:医疗保健