In Java, to convert a String to BigInteger you use the constructor new BigInteger(String)
but to convert an int/long you use the factory function BigInteger.valueof(long)
, why is that?
There actually is a BigInteger(long)
constructor, but it's private. The javadoc on the factory method provides info on why:
This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.
@Morad you can find the answer in the docs: JavaDoc of BigInteger.valueOf(long):
This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.
Explained: BigInteger.valueOf(long)
does exactly what you would expect from the BigInteger(long)
constructor, and it is (or should be) more efficient at it.
There actually is a BigInteger(long)
constructor, but it's private. The javadoc on the factory method provides info on why:
This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.
© 2022 - 2024 — McMap. All rights reserved.