Java 如何使用 BigInteger?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1783912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:38:21  来源:igfitidea点击:

How to use BigInteger?

javabiginteger

提问by cc.

I have this piece of code, which is not working:

我有这段代码,它不起作用:

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {
        sum.add(BigInteger.valueOf(i));
    }
}

The sum variable is always 0. What am I doing wrong?

sum 变量始终为 0。我做错了什么?

采纳答案by MarkPowell

BigIntegeris immutable. The javadocs states that add()"[r]eturns a BigInteger whose value is (this + val)." Therefore, you can't change sum, you need to reassign the result of the addmethod to sumvariable.

BigInteger是不可变的。javadocs 声明add()“[r] 返回一个 BigInteger,其值为 (this + val)。” 因此,您无法更改sum,您需要将add方法的结果重新分配给sum变量。

sum = sum.add(BigInteger.valueOf(i));

回答by Bozho

sum = sum.add(BigInteger.valueOf(i))

The BigIntegerclass is immutable, hence you can't change its state. So calling "add" creates a new BigInteger, rather than modifying the current.

这个BigInteger类是不可变的,因此你不能改变它的状态。所以调用“add”会创建一个新的BigInteger,而不是修改当前的。

回答by Poindexter

BigInteger is an immutable class. So whenever you do any arithmetic, you have to reassign the output to a variable.

BigInteger 是一个不可变的类。因此,无论何时进行任何算术运算,都必须将输出重新分配给变量。

回答by Dean J

Other replies have nailed it; BigInteger is immutable. Here's the minor change to make that code work.

其他回复已经说明了这一点;BigInteger 是不可变的。这是使该代码工作的小改动。

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {
        sum = sum.add(BigInteger.valueOf(i));
    }
}

回答by Arvind

java.math.BigIntegeris an immutableclass so we can not assign new object in the location of already assigned object. But you can create new object to assign new value like:

java.math.BigInteger是一个不可变的类,所以我们不能在已分配对象的位置分配新对象。但是您可以创建新对象来分配新值,例如:

sum = sum.add(BigInteger.valueOf(i));

回答by frank.liu

Since you are summing up some int values together, there is no need to use BigInteger. longis enough for that. intis 32 bits, while longis 64 bits, that can contain the sum of all int values.

由于您将一些 int 值汇总在一起,因此无需使用 BigInteger。long足够了。int是 32 位,而long是 64 位,可以包含所有 int 值的总和。

回答by murali krish

Yes it's Immutable

是的,它是不可变的

sum.add(BigInteger.valueOf(i));

so the method add() of BigInteger class does not add new BigIntger value to its own value ,but creates and returns a new BigInteger referencewithout changing the current BigInteger and this is what done even in the case of Strings

所以 BigInteger 类的 add() 方法不会将新的 BigIntger 值添加到它自己的值中,而是不更改当前 BigInteger的情况下创建并返回一个新的 BigInteger 引用即使在字符串的情况下也是如此

回答by harry

Actually you can use,

其实你可以用,

BigInteger sum= new BigInteger("12345");

for creating object for BigInteger class.But the problem here is,you cannot give a variable in the double quotes.So we have to use the valueOf()method and we have to store the answer in that sum again.So we will write,

用于为 BigInteger 类创建对象。但这里的问题是,你不能在双引号中给出变量。所以我们必须使用valueOf()方法,我们必须再次将答案存储在那个总和中。所以我们会写,

sum= sum.add(BigInteger.valueOf(i));

回答by Arpna Joshi

Bigintegeris an immutable class. You need to explicitly assign value of your output to sum like this:

Biginteger是一个不可变的类。您需要像这样明确地将输出值分配给总和:

sum = sum.add(BigInteger.valueof(i));