user13601721
user13601721

Reputation:

Why don't we use new keyword while converting int to bigInteger whereas we use new keyword in conversion from string to bigInteger?

In the conversion from int to BigInteger, we write

int n = 100;
BigInteger bi = BigInteger.valueOf(n);

when we print bi, we get 100. but, in the conversion from String to BigInteger, we write

String str = "100";
BigInteger biStr = new BigInteger(str);

biStr object uses the new keyword to store the value of the BigInteger value that is converted from a string, but where will the bi object store its BigInteger value that is converted from int.

Upvotes: 0

Views: 45

Answers (1)

Kevin Anderson
Kevin Anderson

Reputation: 4592

Quoting the BigInteger javadoc for valueOf:

"This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers."

Upvotes: 1

Related Questions