Search code examples
javabigintegerfactory-pattern

Why is there BigInteger(String) but no BigInteger(long)?


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?


Solution

  • 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.