问题
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?
回答1:
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.
回答2:
@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.
来源:https://stackoverflow.com/questions/26186277/why-is-there-bigintegerstring-but-no-bigintegerlong