合肥做网站公,五八同城58同城找工作,局网站内容建设考核,做外贸有哪些好的网站有哪些内容对JDK 8中的新功能的关注理所当然地主要集中在新的语言功能和语法上。 但是#xff0c;对库和API进行了一些不错的添加#xff0c;在本文中#xff0c;我介绍了BigInteger类中添加的四个新方法#xff1a; longValueExact#xff08;#xff09; #xff0c; intValueEx… 对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 BigIntegers 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方法将不会显示不正确的表示形式。 尽管异常通常不是我们最喜欢的事情之一但它们总是比获取和使用错误的数据甚至不意识到它是错误的更好。 翻译自: https://www.javacodegeeks.com/2014/04/new-biginteger-methods-in-java-8.html