Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JDK 8 BigInteger Exact Narrowing Conversion Methods

DZone's Guide to

JDK 8 BigInteger Exact Narrowing Conversion Methods

When it comes to performing a narrowing conversion using BigInteger in Java 8, here is some information that made this process a lot easier.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In the blog post, "Exact Conversion of Long to Int in Java," I discussed using Math.toIntExact(Long) to convert a Long to an int , using an ArithmeticException if this narrowing conversion is not possible. That method was introduced with JDK 8, which also introduced similar narrowing conversion methods to the BigInteger class. Those BigInteger methods are the topics of this post.

BigInteger had four new "exact" methods added to it in JDK 8:

As described above, each of these four "exact" methods added to BigInteger with JDK 8 allow for the BigInteger's value to be narrowed to the data type in the method name, if that is possible. Because all of these types ( byte, short, int, and long) have smaller ranges than BigInteger, it's possible for any of these cases to have a value in BigDecimal with a magnitude larger than that which can be represented by any of these four types. In such a case, all four of these "Exact" methods throw an ArithmeticException , rather than quietly "forcing" the bigger value into the smaller representation, which is typically a nonsensical number for most contexts.

Examples of using these methods can be found on GitHub. When those examples are executed, the output looks like this:


===== Byte =====
125 => 125
126 => 126
127 => 127
128 => java.lang.ArithmeticException: BigInteger out of byte range
129 => java.lang.ArithmeticException: BigInteger out of byte range
===== Short =====
32765 => 32765
32766 => 32766
32767 => 32767
32768 => java.lang.ArithmeticException: BigInteger out of short range
32769 => java.lang.ArithmeticException: BigInteger out of short range
===== Int =====
2147483645 => 2147483645
2147483646 => 2147483646
2147483647 => 2147483647
2147483648 => java.lang.ArithmeticException: BigInteger out of int range
2147483649 => java.lang.ArithmeticException: BigInteger out of int range
===== Long =====
9223372036854775805 => 9223372036854775805
9223372036854775806 => 9223372036854775806
9223372036854775807 => 9223372036854775807
9223372036854775808 => java.lang.ArithmeticException: BigInteger out of long range
9223372036854775809 => java.lang.ArithmeticException: BigInteger out of long range

The addition of these "exact" methods to BigInteger with JDK 8 is a great addition, because errors associated with numeric narrowing and overflow have been subtle. It's nice to have an easy way to get an "exact" narrowing or to at least have the inability to do narrowing made obvious via an exception.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,java 8 ,conversion ,biginteger ,integer ,long ,code ,how-to

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}