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

你。 提交于 2019-12-23 08:44:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!