对JDK 8中的新功能的关注理所当然地主要集中在新的语言功能和语法上。 但是,对库和API进行了一些不错的添加,在本文中,我介绍了BigInteger类中添加的四个新方法: longValueExact() , intValueExact() , shortValueExact()和byteValueExact() 。

如果BigInteger实例中包含的数字不能以指定的形式(在方法的名称中指定)提供而又不丢失信息,则所有新引入的所有“ xxxxxExact()”方法都将引发ArithmeticException 。 BigInteger已经拥有方法intValue()和longValue()以及从(继承自Number的)方法shortValue()和byteValue() 。 如果BigInteger值作为这些类型之一丢失表示中的信息,则这些方法不会引发异常。 尽管乍看之下似乎是一种优势,但这意味着使用这些方法的结果的代码使用的值不准确,而又无法知道信息已丢失。 新的“ xxxxxExact”方法将引发ArithmenticException而不是假装提供丢失大量信息的结果。

以下简单的代码清单演示了“传统”方法,该方法以byteshortintlong类型显示错误数据,而不是引发异常。 相同的代码还演示了新的“ xxxxxExact”方法的使用,这些方法会在信息丢失时抛出异常,而不是呈现错误的表示形式。 运行此代码的输出紧随该代码之后,并说明了BigInteger包含一个值比返回的byteshortintlong所表示的信息更多的值时,方法如何不同。

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方法将不会显示不正确的表示形式。 尽管异常通常不是我们最喜欢的事情之一,但它们总是比获取和使用错误的数据甚至不意识到它是错误的更好。

翻译自: https://www.javacodegeeks.com/2014/04/new-biginteger-methods-in-java-8.html

Java 8中的新BigInteger方法相关推荐

  1. 【Java 8 新特性】Java 8中的Function.apply方法

    Java 8中的Function.apply方法 参考文献 java.util.function.Function是一个接口,已经在 Java 8中引入. Function是一个函数接口. 因此它可以 ...

  2. Java 11中的新功能和API详解系列1

    Java 11中的新功能和API详解系列1 2018.9.27 版权声明:本文为博主chszs的原创文章,未经博主允许不得转载. JDK 11在语言语法方面有一个小改动,增加了相当数量的新API,以及 ...

  3. 【译】使用示例带你提前了解 Java 9 中的新特性

    Java 作为 Android 的基础编程语言,每一次迭代也是备受安卓开发人员的关注.这不,Oracle 公司在今年即将发布 Java 9 正式版,一些新的特性和改进很是值得期待. 周末时间,拜读了国 ...

  4. java工作中好用的方法

    java工作中好用的方法 1.copyProperties() 复制对象参数的方法,当两个对象中的参数有一部分是一致的时候,用这个方法,不需要一个一个get,set参数了. BeanUtils.cop ...

  5. java pattern matches,Java正则表达式中的Pattern.matches()方法

    java.util.regex.Pattern.matches()方法匹配正则表达式和给定的输入.它有两个参数,即正则表达式和输入.如果正则表达式和输入匹配,则返回true,否则返回false. 给出 ...

  6. 你还在 Java 代码中写 set/get 方法?赶快试试这款插件吧!

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:Mr.ml https://blog.csdn.net/Ma ...

  7. Java 9中的新Regex功能

    最近,我收到了Packt出版的Anubhava Srivastava提供的免费书籍" Java 9 Regular Expressions" . 这本书是一个很好的教程,它向任何想 ...

  8. JDK 17:Java 17 中的新特性

    始终严格的浮点语义.外部函数和内存 API 以及伪随机数生成器的统一 API 计划用于 9 月发布的 Java 长期支持版本. Java 17 的功能集现已冻结,标准 Java 的下一版本将拥有 10 ...

  9. java jdk12_JDK 12:Java 12中的新功能

    java jdk12 基于Java SE(标准版)12的Java开发套件12的生产版本现已发布. Oracle可以为Linux,Windows和MacOS提供JDK 12构建. [ Java JDK ...

最新文章

  1. 女性程序员占比超17%,平均月薪近2万 | 程序员就业大数据报告
  2. 强化学习的自然环境基准
  3. gan只用来生成是浪费
  4. gdb调试fork+exec创建的子进程的方法
  5. HTML5本地存储localstorage
  6. mqtt 域名连接_中国移动OneNet物联网平台,如何使用MQTT协议,进行连接
  7. Java中HashMap、LinkedHashMap和TreeMap区别使用场景
  8. spark读取csv转化为rdd(python+scala+java三种代码汇总)
  9. 非静态成员函数的非法调用错误
  10. [Vue.js]实战 -- 电商项目(五)
  11. 移动设备应用程序中支持多个屏幕大小和 DPI 值
  12. linux下的微博客户端,Linux下非官方的新浪微博客户端:WeCase(微盒),附安装方法...
  13. 如何用c++画图_看 FILA 与白山如何用经典黑白演奏不一样的C大调!
  14. SBC音频编解码算法(转载)
  15. 动态图片怎么制作 html,怎么制作动态图片
  16. 校验非空的注解@NotNull怎么取得自定义的message
  17. mvn No proxies configured downloading directly
  18. TI单芯片毫米波雷达1642代码走读(〇)——总纲
  19. mysql trans begin_[原创]MySQL RR隔离级别下begin或start transaction开启事务后的可重复读?...
  20. 岭回归实现鲍鱼年龄预测 MATLAB实现

热门文章

  1. 脚本可以放在html外,关于把script脚本放在html结束标签外的运行结果???
  2. java 时钟 算法分析_java实现时钟方法汇总
  3. php 正则获取html标签,php正则取嵌套html标签
  4. k8s往secret里导入证书_K8S之Secret
  5. MySQL优化(一):表结构优化
  6. Angular项目打包到nginx部署过程
  7. log4j 程序日志_Log4j错误–减慢您的应用程序
  8. vue框架项目部署到服务器_在浏览器中在线尝试无服务器框架项目!
  9. pom.xml中pom全称_摆脱pom XML…几乎
  10. flex布局水平垂直 垂直_垂直和水平装饰