Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Why doesn't Java's BigInteger class have a constructor capable of taking a numeric literal? Every single time I use BigIntegers, and many times I merely think about them, I wonder this.

What reason could the designers of java have had to exclude one despite the overwhelming convenience of one should it exist?

share|improve this question

closed as not constructive by Robert Harvey, kevin cline, Walter, GlenH7, Yusubov Oct 29 '12 at 23:00

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Because you can easily call toString on an integral type? And doing so would require multiple overloads - one per integral type. –  Oded Oct 29 '12 at 19:50
    
I think one constructor taking a long would suffice, since a numeric literal can automatically cast to a long. Java will let you cast a type to a more precise (not efficient, derp) type implicitly. It kind of makes sense for people who want to build one from a floating point type to have to at least cast it, but I don't see why we force people who want to make a biginteger with a value of 3 suffer. –  Wug Oct 29 '12 at 19:52
    
@kevincline: I'm not saying remove the string constructor, I'm asking why there isn't a long one. –  Wug Oct 29 '12 at 19:55

1 Answer 1

The answer lies in the JavaDoc of BigInteger.valueOf(long):

This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers.

In other words: BigInteger.valueOf(long) does exactly what you ask the hypothetical BigInteger(long) constructor to do, except it is (or at least can be) slightly more efficient at it.

share|improve this answer
3  
Excellent. Have some precious Internet reps. +1 –  Robert Harvey Oct 29 '12 at 20:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.