对JDK 8中的新功能的关注理所当然地主要集中在新的语言功能和语法上。 但是,对库和API进行了一些不错的添加,在本文中,我介绍了BigInteger类中添加的四个新方法: longValueExact() , intValueExact() , shortValueExact()和byteValueExact() 。
如果BigInteger
实例中包含的数字不能以指定的形式(在方法的名称中指定)提供而又不丢失信息,则所有新引入的所有“ xxxxxExact()”方法都将引发ArithmeticException 。 BigInteger
已经拥有方法intValue()和longValue()以及从(继承自Number的)方法shortValue()和byteValue() 。 如果BigInteger
值作为这些类型之一丢失表示中的信息,则这些方法不会引发异常。 尽管乍看之下似乎是一种优势,但这意味着使用这些方法的结果的代码使用的值不准确,而又无法知道信息已丢失。 新的“ xxxxxExact”方法将引发ArithmenticException
而不是假装提供丢失大量信息的结果。
以下简单的代码清单演示了“传统”方法,该方法以byte
, short
, int
和long
类型显示错误数据,而不是引发异常。 相同的代码还演示了新的“ xxxxxExact”方法的使用,这些方法会在信息丢失时抛出异常,而不是呈现错误的表示形式。 运行此代码的输出紧随该代码之后,并说明了BigInteger
包含一个值比返回的byte
, short
, int
或long
所表示的信息更多的值时,方法如何不同。
BigIntegerDem.java
package dustin.examples.jdk8;
import static java.lang.System.out;
import java.math.BigInteger;
/**
* Demonstrate the four new methods of BigInteger introduced with JDK 8.
*
* @author Dustin
*/
public class BigIntegerDemo
{
/**
* Demonstrate BigInteger.byteValueExact().
*/
private static void demonstrateBigIntegerByteValueExact()
{
final BigInteger byteMax = new BigInteger(String.valueOf(Byte.MAX_VALUE));
out.println("Byte Max: " + byteMax.byteValue());
out.println("Byte Max: " + byteMax.byteValueExact());
final BigInteger bytePlus = byteMax.add(BigInteger.ONE);
out.println("Byte Max + 1: " + bytePlus.byteValue());
out.println("Byte Max + 1: " + bytePlus.byteValueExact());
}
/**
* Demonstrate BigInteger.shortValueExact().
*/
private static void demonstrateBigIntegerShortValueExact()
{
final BigInteger shortMax = new BigInteger(String.valueOf(Short.MAX_VALUE));
out.println("Short Max: " + shortMax.shortValue());
out.println("Short Max: " + shortMax.shortValueExact());
final BigInteger shortPlus = shortMax.add(BigInteger.ONE);
out.println("Short Max + 1: " + shortPlus.shortValue());
out.println("Short Max + 1: " + shortPlus.shortValueExact());
}
/**
* Demonstrate BigInteger.intValueExact().
*/
private static void demonstrateBigIntegerIntValueExact()
{
final BigInteger intMax = new BigInteger(String.valueOf(Integer.MAX_VALUE));
out.println("Int Max: " + intMax.intValue());
out.println("Int Max: " + intMax.intValueExact());
final BigInteger intPlus = intMax.add(BigInteger.ONE);
out.println("Int Max + 1: " + intPlus.intValue());
out.println("Int Max + 1: " + intPlus.intValueExact());
}
/**
* Demonstrate BigInteger.longValueExact().
*/
private static void demonstrateBigIntegerLongValueExact()
{
final BigInteger longMax = new BigInteger(String.valueOf(Long.MAX_VALUE));
out.println("Long Max: " + longMax.longValue());
out.println("Long Max: " + longMax.longValueExact());
final BigInteger longPlus = longMax.add(BigInteger.ONE);
out.println("Long Max + 1: " + longPlus.longValue());
out.println("Long Max + 1: " + longPlus.longValueExact());
}
/**
* Demonstrate BigInteger's four new methods added with JDK 8.
*
* @param arguments Command line arguments.
*/
public static void main(final String[] arguments)
{
System.setErr(out); // exception stack traces to go to standard output
try
{
demonstrateBigIntegerByteValueExact();
}
catch (Exception exception)
{
exception.printStackTrace();
}
try
{
demonstrateBigIntegerShortValueExact();
}
catch (Exception exception)
{
exception.printStackTrace();
}
try
{
demonstrateBigIntegerIntValueExact();
}
catch (Exception exception)
{
exception.printStackTrace();
}
try
{
demonstrateBigIntegerLongValueExact();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}
输出
Byte Max: 127
Byte Max: 127
Byte Max + 1: -128
java.lang.ArithmeticException: BigInteger out of byte range
at java.math.BigInteger.byteValueExact(BigInteger.java:4428)
at dustin.examples.jdk8.BigIntegerDemo.demonstrateBigIntegerByteValueExact(BigIntegerDemo.java:23)
at dustin.examples.jdk8.BigIntegerDemo.main(BigIntegerDemo.java:75)
Short Max: 32767
Short Max: 32767
Short Max + 1: -32768
java.lang.ArithmeticException: BigInteger out of short range
at java.math.BigInteger.shortValueExact(BigInteger.java:4407)
at dustin.examples.jdk8.BigIntegerDemo.demonstrateBigIntegerShortValueExact(BigIntegerDemo.java:36)
at dustin.examples.jdk8.BigIntegerDemo.main(BigIntegerDemo.java:84)
Int Max: 2147483647
Int Max: 2147483647
Int Max + 1: -2147483648
java.lang.ArithmeticException: BigInteger out of int range
at java.math.BigInteger.intValueExact(BigInteger.java:4386)
at dustin.examples.jdk8.BigIntegerDemo.demonstrateBigIntegerIntValueExact(BigIntegerDemo.java:49)
at dustin.examples.jdk8.BigIntegerDemo.main(BigIntegerDemo.java:93)
Long Max: 9223372036854775807
Long Max: 9223372036854775807
Long Max + 1: -9223372036854775808
java.lang.ArithmeticException: BigInteger out of long range
at java.math.BigInteger.longValueExact(BigInteger.java:4367)
at dustin.examples.jdk8.BigIntegerDemo.demonstrateBigIntegerLongValueExact(BigIntegerDemo.java:62)
at dustin.examples.jdk8.BigIntegerDemo.main(BigIntegerDemo.java:102)
如上面的输出所示,当返回的类型无法在BigInteger
实例中保存信息时,名称中带有“ xxxxxExact”的新BigInteger方法将不会显示不正确的表示形式。 尽管异常通常不是我们最喜欢的事情之一,但它们总是比获取和使用错误的数据甚至不意识到它是错误的更好。
翻译自: https://www.javacodegeeks.com/2014/04/new-biginteger-methods-in-java-8.html