BigInteger :: intValueExact() - 有什么意义?

时间:2021-11-07 16:51:26

I noticed that the class BigInteger received a new method in Java 8: intValueExact().

我注意到BigInteger类在Java 8中收到了一个新方法:intValueExact()。

My question is: why?

我的问题是:为什么?

BigInteger bigInt = ... ;

bigInt.intValueExact();

BigInteger already had intValue(). The intValueExact() is meant to throw an error when your BigInteger does not hold an exact int value, but my question is: how could it be possible to give a BigInteger a value that will not equate to an exact int ? Can someone provide an example of when this method would throw an ArithmeticException ??

BigInteger已经有了intValue()。 intValueExact()用于在BigInteger不保存精确的int值时抛出错误,但我的问题是:怎么可能给BigInteger一个不等于精确int的值呢?有人可以提供一个示例,说明此方法何时会抛出ArithmeticException?

3 个解决方案

#1


9  

new BigInteger("10000000000000000000000000000000000000000000000").intValueExact()

If the BigInteger is too big an integer to fit into an int, the exception is thrown.

如果BigInteger太大而不适合int,则抛出异常。

#2


9  

The intValue() method will only keep the lowest 32 bits that will fit in an int, discarding information if necessary.

intValue()方法只保留适合int的最低32位,必要时丢弃信息。

Converts this BigInteger to an int. This conversion is analogous to a narrowing primitive conversion from long to int as defined in section 5.1.3 of The Java™ Language Specification: if this BigInteger is too big to fit in an int, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.

将此BigInteger转换为int。这种转换类似于在Java™语言规范的5.1.3节中定义的从long到int的缩小原语转换:如果这个BigInteger太大而不适合int,则只返回低位32位。请注意,此转换可能会丢失有关BigInteger值的总体大小的信息,并返回具有相反符号的结果。

The intValueExact() method will throw an exception in this case rather than give you a different value.

在这种情况下,intValueExact()方法将抛出异常,而不是为您提供不同的值。

Converts this BigInteger to an int, checking for lost information. If the value of this BigInteger is out of the range of the int type, then an ArithmeticException is thrown.

将此BigInteger转换为int,检查丢失的信息。如果此BigInteger的值超出int类型的范围,则抛出ArithmeticException。

#3


1  

From the doc:

从文档:

Converts this BigInteger to an int, checking for lost information. If the value of this BigInteger is out of the range of the int type, then an ArithmeticException is thrown.

将此BigInteger转换为int,检查丢失的信息。如果此BigInteger的值超出int类型的范围,则抛出ArithmeticException。

So quite simply if the value of the BigInteger is greater than 2^31 - 1 then an ArithmeticException is thrown.

很简单,如果BigInteger的值大于2 ^ 31 - 1,则抛出ArithmeticException。

#1


9  

new BigInteger("10000000000000000000000000000000000000000000000").intValueExact()

If the BigInteger is too big an integer to fit into an int, the exception is thrown.

如果BigInteger太大而不适合int,则抛出异常。

#2


9  

The intValue() method will only keep the lowest 32 bits that will fit in an int, discarding information if necessary.

intValue()方法只保留适合int的最低32位,必要时丢弃信息。

Converts this BigInteger to an int. This conversion is analogous to a narrowing primitive conversion from long to int as defined in section 5.1.3 of The Java™ Language Specification: if this BigInteger is too big to fit in an int, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.

将此BigInteger转换为int。这种转换类似于在Java™语言规范的5.1.3节中定义的从long到int的缩小原语转换:如果这个BigInteger太大而不适合int,则只返回低位32位。请注意,此转换可能会丢失有关BigInteger值的总体大小的信息,并返回具有相反符号的结果。

The intValueExact() method will throw an exception in this case rather than give you a different value.

在这种情况下,intValueExact()方法将抛出异常,而不是为您提供不同的值。

Converts this BigInteger to an int, checking for lost information. If the value of this BigInteger is out of the range of the int type, then an ArithmeticException is thrown.

将此BigInteger转换为int,检查丢失的信息。如果此BigInteger的值超出int类型的范围,则抛出ArithmeticException。

#3


1  

From the doc:

从文档:

Converts this BigInteger to an int, checking for lost information. If the value of this BigInteger is out of the range of the int type, then an ArithmeticException is thrown.

将此BigInteger转换为int,检查丢失的信息。如果此BigInteger的值超出int类型的范围,则抛出ArithmeticException。

So quite simply if the value of the BigInteger is greater than 2^31 - 1 then an ArithmeticException is thrown.

很简单,如果BigInteger的值大于2 ^ 31 - 1,则抛出ArithmeticException。