简体   繁体   中英

Initializing java.math.BigInteger

Sorry cause this might look like a stupid yes or no question but I'm very new to this so I need an answer.

BigInteger i = BigInteger.valueOf(0);

and

BigInteger i = new BigInteger("0");

Are they the same?

They both end up with a reference to a BigInteger with a value of 0, but they're not identical in effect. In particular, as valueOf is a static method, it can make use of caching, and return the same reference if you call it twice:

BigInteger a = BigInteger.valueOf(0);
BigInteger b = BigInteger.valueOf(0);
System.out.println(a == b); // true on my machine

That doesn't appear to be guaranteed , but it's certainly somewhat expected given the documentation :

Returns a BigInteger whose value is equal to that of the specified long. This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.

When you call the constructor, you really will get a new instance every time.

That said, for this particular example, I'd just use BigInteger.ZERO ...

Yes, in this case, they are the same (if by "same" you mean "instances that are equal"), and you could also say BigInteger.ZERO .

But in the case of really big numbers you can only use the String constructor:

new BigInteger("12345678901234567890123456789012345")  // too long for long
  BigInteger i = BigInteger.valueOf(0);
                BigInteger i1 = new BigInteger("0");
                System.out.println(i==i1);//false
                System.out.println(i.equals(i1));//true

Just take a look at the documentation for both the method and the constructor .

public static BigInteger valueOf(long val)
Returns a BigInteger whose value is equal to that of the specified long. This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.

Parameters: val - value of the BigInteger to return.
Returns: a BigInteger with the specified value.

BigInteger(String val)
Translates the decimal String representation of a BigInteger into a BigInteger.

They both end up with a reference to a BigInteger with a value as 0 .

  1. The valueOf is static, so you don't have to create an object in order to get the value.
  2. If you call the constructor, then you do get a new object each time.

Lets do a quick experiment:

@Test
public void testBigIntegers() {
    assertThat(BigInteger.valueOf(0), is(new BigInteger("0")));
}

So, to be precise: you have two equal BigInteger objects here. As the BigInteger constructor only allows for entering "whole" integers; that is true for all values that valueOf() would be able to give you.

But because those two objects are created in different ways, you really have two different objects here. Whereas calling valueOf(0) twice might very well give the same object (reference) for both calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM