Java 8中的新BigInteger方法
對JDK 8中的新功能的關注理所當然地主要集中在新的語言功能和語法上。 但是,對庫和API進行了一些不錯的添加,在本文中,我介紹了BigInteger類中添加的四個新方法: longValueExact() , intValueExact() , shortValueExact()和byteValueExact() 。
如果BigInteger實例中包含的數(shù)字不能以指定的形式(在方法的名稱中指定)提供而又不丟失信息,則所有新引入的所有“ xxxxxExact()”方法都將引發(fā)ArithmeticException 。 BigInteger已經(jīng)擁有方法intValue()和longValue()以及從(繼承自Number的)方法shortValue()和byteValue() 。 如果BigInteger值作為這些類型之一丟失表示中的信息,則這些方法不會引發(fā)異常。 盡管乍看之下似乎是一種優(yōu)勢,但這意味著使用這些方法的結(jié)果的代碼使用的值不準確,而又無法知道信息已丟失。 新的“ xxxxxExact”方法將引發(fā)ArithmenticException而不是假裝提供丟失大量信息的結(jié)果。
以下簡單的代碼清單演示了“傳統(tǒng)”方法,該方法以byte , short , int和long類型顯示錯誤數(shù)據(jù),而不是引發(fā)異常。 相同的代碼還演示了新的“ xxxxxExact”方法的使用,這些方法會在信息丟失時拋出異常,而不是呈現(xiàn)錯誤的表示形式。 運行此代碼的輸出緊隨該代碼之后,并說明了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 outputtry{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 rangeat 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 rangeat 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 rangeat 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 rangeat 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方法將不會顯示不正確的表示形式。 盡管異常通常不是我們最喜歡的事情之一,但它們總是比獲取和使用錯誤的數(shù)據(jù)甚至不意識到它是錯誤的更好。
翻譯自: https://www.javacodegeeks.com/2014/04/new-biginteger-methods-in-java-8.html
總結(jié)
以上是生活随笔為你收集整理的Java 8中的新BigInteger方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 湖南电信遭遇ddos攻击怎么办(湖南电信
- 下一篇: linux常用命令大全新手入门(linu