jdk 取整数
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 取整数