154

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?

Ivar
  • 6,138
  • 12
  • 49
  • 61
cc.
  • 5,533
  • 14
  • 37
  • 46
  • 1
    By the way, the sum should easily fit in `int`, so you don't need `BigInteger` for this example. – notnoop Nov 23 '09 at 15:51
  • 8
    Nope, I changed the code. The number is bigger than 5000. – cc. Nov 23 '09 at 19:40
  • The question linked as duplicate does not seem to have the same problem as this question (the linked question is about *which* function to use so BigInteger can be added, this one is about *how* to use the add function) – justhalf Dec 23 '15 at 14:04

10 Answers10

207

BigInteger is 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 add method to sum variable.

sum = sum.add(BigInteger.valueOf(i));
Community
  • 1
  • 1
MarkPowell
  • 16,482
  • 7
  • 61
  • 77
  • 1
    int will be enough as long as you don't go over 2^31-1, long will be enough as long as you don't go over 2^63-1. – Jean Hominal Nov 23 '09 at 16:45
  • 2
    Which, in his example, he won't. – MarkPowell Nov 23 '09 at 16:46
  • 108
    But is it really that hard to think perhaps he simplified his example down to exactly what the problem is? – thecoshman Jul 24 '13 at 10:46
  • 1
    @thecoshman - You are quite correct and the number of upvotes on your comment show this is wise advice for all readers of such questions. Some more wise advice is "**read what others wrote before answering or commenting.**" For example in this case it doesn't even require *ANY* thought since the OP clearly stated that he did just that in the comments below the Question: "*Nope, I changed the code. The number is bigger than 5000.*" – O.M.Y. Apr 08 '18 at 18:29
58
sum = sum.add(BigInteger.valueOf(i))

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
23

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

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {
        sum = sum.add(BigInteger.valueOf(i));
    }
}
Dean J
  • 39,360
  • 16
  • 67
  • 93
12

java.math.BigInteger is an immutable class 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:

sum = sum.add(BigInteger.valueOf(i));
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
Arvind
  • 1,548
  • 17
  • 23
12

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

Poindexter
  • 2,396
  • 16
  • 19
4

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 reference without changing the current BigInteger and this is what done even in the case of Strings

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
murali kurapati
  • 1,510
  • 18
  • 23
0

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,

sum= sum.add(BigInteger.valueOf(i));
harry
  • 45
  • 1
  • 10
0

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

sum = sum.add(BigInteger.valueof(i));    
Mike Stockdale
  • 5,256
  • 3
  • 29
  • 33
Arpna Joshi
  • 37
  • 1
  • 6
  • 4
    This is now the 8th answer with the same explanation, so how is this answer helpful? – Tom Oct 09 '16 at 03:57
0

Suppose sometimes our input value is too big to store integer value eg.123456789123456789 In that case, standard datatype like long can't handle it. But Java provides a class "BigInteger". It helps us to pass and access huge value. Please see the below exam to clear the concept.

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        BigInteger B1=new BigInteger("10");
        BigInteger B2=new BigInteger("20");
        B1=sc.nextBigInteger();
        B2=sc.nextBigInteger();
        Solution obj=new Solution();
        obj.Big_Int(B1,B2);
        sc.close();
    }
    
    public void Big_Int(BigInteger a,BigInteger b)
    {
        //BigInteger bi=new BigInteger("1");
        BigInteger bi=BigInteger.TEN;
        bi=a;
        bi=bi.add(b);
        System.out.println(bi);
        bi=a;
        bi=bi.multiply(b);
        System.out.println(bi);
        
    }
}

input: 123456789123456789 1578426354785

output: 123458367549811574 194867449629598334799730885365

-6

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

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
frank.liu
  • 477
  • 1
  • 5
  • 11
  • "But is it really that hard to think perhaps he simplified his example down to exactly what the problem is?" (quoting thecoshman) – Bulwersator Jan 03 '14 at 07:22
  • 5
    For this question, my answer is a bit our of scope. Since the topic focus on how to use BigInteger. Just one of my personal experience, if we want to sum up some integers and the numbers are not pretty big, I would prefer long. Because that's easy to use and runs faster. For large scale input, BigInteger is the good choice. – frank.liu Jan 07 '14 at 06:33