Monday, August 29, 2016

Very BigInteger

java.lang.BigInteger is very helpful but I think we should use this class with correct implementation.
If you use this class for keeping Integer values and convert them  into int type you need checking the returned value if it is too big for integer. According to specification,  soft warning is shown but many developers omit this information mostly.


intValue

public int intValue()
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.
Specified by:
intValue in class Number
Returns:
this BigInteger converted to an int.


Source: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html