简体   繁体   English

初始化java.math.BigInteger

[英]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. 它们最终都引用了一个值为0的BigInteger ,但它们的效果并不相同。 In particular, as valueOf is a static method, it can make use of caching, and return the same reference if you call it twice: 特别是,由于valueOf是一个静态方法,它可以使用缓存,如果你调用它两次,则返回相同的引用:

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. 返回一个BigInteger其值等于指定long的值。 This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers. 这种“静态工厂方法”优先于(长)构造函数提供,因为它允许重用常用的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 ... 也就是说,对于这个特例,我只使用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 . 是的,在这种情况下,它们是相同的(如果用“相同”表示“相等的实例”),你也可以说BigInteger.ZERO

But in the case of really big numbers you can only use the String constructor: 但是对于非常大的数字,你只能使用String构造函数:

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) public static BigInteger valueOf(long val)
Returns a BigInteger whose value is equal to that of the specified long. 返回一个BigInteger,其值等于指定long的值。 This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers. 这种“静态工厂方法”优先于(长)构造函数提供,因为它允许重用常用的BigIntegers。

Parameters: val - value of the BigInteger to return. 参数: val - 要返回的BigInteger的值。
Returns: a BigInteger with the specified value. 返回:具有指定值的BigInteger。

BigInteger(String val) BigInteger(String val)
Translates the decimal String representation of a BigInteger into a BigInteger. 将BigInteger的十进制字符串表示形式转换为BigInteger。

They both end up with a reference to a BigInteger with a value as 0 . 它们最终都引用了一个值为0BigInteger

  1. The valueOf is static, so you don't have to create an object in order to get the value. valueOf是静态的,因此您无需创建对象即可获取值。
  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. 所以,准确地说:这里有两个相同的 BigInteger对象。 As the BigInteger constructor only allows for entering "whole" integers; 由于BigInteger构造函数只允许输入“整数”整数; that is true for all values that valueOf() would be able to give you. 对于valueOf()能够为您提供的所有值都是如此。

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. 而调用valueOf(0)两次可能会为两个调用提供相同的对象(引用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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