I wrote a test code to check equality. I have checked Java doc and it says BigInteger
is Immutable. Checking the documentation of the static factory method valueOf
it looks like it returns the already cached immutable instance. So why does == returns false when its the cached instance.
Below is the Java doc for valueOf
in BigInteger
:
Returns a Big Integer whose value is equal to that of the specified long. This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.
The below code is going into infinite loop .
public static void main(String[] args) {
while(true) {
BigInteger a = BigInteger.valueOf(100);
BigInteger c = BigInteger.valueOf(100);
if (a == c) {
break;
}
}