trying to add two BigInterger Numbers together in a class Constructor and gettiing stackoverflow error

trying to add two BigInterger Numbers together in a class Constructor and gettiing stackoverflow error.
import java.math.BigInteger;

public class BigNumber implements Cloneable {

    BigInteger sum = BigInteger.valueOf(0);
    BigInteger sum2 = BigInteger.valueOf(0);

    BigNumber(String g) {
        sum = sum.add(new BigInteger(g));
    }

    public String ToString() {
        String a = "" + sum;
        return a;
    }

    public Object CloneNumber() throws CloneNotSupportedException {
        return super.clone();
    }

    public BigNumber add(BigNumber other) throws CloneNotSupportedException {
        return ((BigNumber) BigNumber.this.CloneNumber()).add(other);
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        BigNumber g = new BigNumber("46376937677490009712648124896970078050417018260538");
        BigNumber j = new BigNumber("37107287533902102798797998220837590246510135740250").add(g);
        String f = j.ToString();
        System.out.print(f);
    }

}
Jon Skeet
people
quotationmark

The stack overflow isn't in your constructor. It's when your main method calls add on your BigNumber.

This is the first problem:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return ((BigNumber) BigNumber.this.CloneNumber()).add(other); 
}

You're calling add within the add method... how would you expect that to work?

It's not clear why you've got both sum and sum2, but I expect your add method only needs to be:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum).toString());
}

... although you'd be better off with an overload of the constructor to accept a BigInteger, at which point you can just have:

public BigNumber add(BigNumber other) throws CloneNotSupportedException {
    return new BigNumber(sum.add(other.sum));
}

The constructors (and field declaration) would then look like this:

// Personally I'd make the class final, but that's a different matter
public class BigNumber {
    private final BigInteger value; // Renamed from sum - see below

    public BigNumber(BigInteger value) {
        this.value = value;
    }

    public BigNumber(String value) {
        this(new BigInteger(value));
    }

    ... methods ...
}

Additionally:

  • You should override the existing toString method rather than introducing your own ToString method
  • You should override clone() instead of introducing your own CloneNumber method
  • Follow Java naming conventions, where methods are camelCased, not PascalCased.
  • You should remove the sum2 field which isn't used anywhere within your class
  • You don't need to add anything in your constructor - you can just have:

    this.sum = new BigInteger(g);
    
  • Your field is called sum but it's not obviously the sum of anything - it's just the value. I would rename it to value or something similar.

  • It's not clear why you're doing this at all - you're just reproducing a small part of the BigInteger class... why?

people

See more on this question at Stackoverflow