No autoboxing for BigInteger?

1.5k views Asked by At

While fixing the code for this question, I realized that autoboxing doesn't work for all types. This code compiles:

Integer y = 3;

But doing the same with BigInteger doesn't compile:

BigInteger x = 3;

-> "Type mismatch: cannot convert from int to BigInteger"

Is there no autoboxing for BigInteger? If not, what is the rule for the types supporting autoboxing and why isn't BigInteger included?

5

There are 5 answers

5
nanofarad On BEST ANSWER

First of all, note that BigInteger is part of java.math and not java.lang, and so would not receive special treatment by the language. All of the boxed types are in java.lang and so the Java language might treat them specially. Such consideration can include boxing, strings in constant pools, class objects living in specialized areas of memory, etc.

Secondly, a reference document called the Java Language Specification (or JLS for short) describes this precisely:

Boxing conversion converts expressions of primitive type to corresponding expressions of reference type. Specifically, the following nine conversions are called the boxing conversions:

  • From type boolean to type Boolean

  • From type byte to type Byte

  • From type short to type Short

  • From type char to type Character

  • From type int to type Integer

  • From type long to type Long

  • From type float to type Float

  • From type double to type Double

  • From the null type to the null type

Source

However, there is a request to allow autoboxing BigInteger and giving special meaning to various mathematical operators when applied to BigInteger objects.

1
Juned Ahsan On

autoboxing works between primitives and their corresponding Wrapper class. As BigInteger is not exactly a wrapper class for int hence the error.

3
rossum On

Read the documentation for BigInteger. You will see that it does not need autoboxing because it is already a class, not a primitive. To do what you want look at the methods provided in the BigInteger class, particularly the static BigInteger.valueOf() method:

BigInteger x = BigInteger.valueOf(3);

1
GhostCat On

Let me answer by putting up a question:

The idea of BitInteger is to represent arbitrary-precision integers. How exactly do you envision to "unbox" objects of such a class into the existing primitive types?

0
Srini On

Is there no autoboxing for BigInteger?

Juned and hexafraction have already pointed out that autoboxing works between primitives and their corresponding Wrappers.

As to why BigInteger doesn't have a corresponding primitive would tantamount to answering your second question:

If not, what is the rule for the types supporting autoboxing and why isn't BigInteger included?

Primitives are variables a CPU supports to operate directly, with BigInteger this isn't possible. This is a class that supports operation with massive numbers, and such operations require considerably more management.

Every modern computer has a machine-language instruction for integer addition. Therefore it can also have very simple byte code in the JVM. A complex type like BigInteger cannot be handled that way, and it cannot be translated into simple byte code. Therefore, it cannot be a primitive.

Since it's a class and Java doesn't support operator overloading, you are required to use its methods and constructors instead of the simple arithmetic operators that you'd be able to use with primitives.