0

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?

Community
  • 1
  • 1
mastov
  • 2,942
  • 1
  • 16
  • 33
  • 6
    There is no unboxed type for BigInteger. Therefore there is no autoboxing. – talex Jun 19 '15 at 13:43
  • 1
    There is an official bug reported for it: http://bugs.java.com/view_bug.do?bug_id=6407464. – Balkrishna Rawool Jun 19 '15 at 13:44
  • @talex: And why would you *need* an exactly corresponding unboxed type? Since conversions are just one-way, couldn't you use `int` or `long` for that? – mastov Jun 19 '15 at 13:49
  • @mastov And why would you expect that a "conversion" called boxing that converts a primitive type into its **semantically equivalent** object type ... to work for a something that doesn't have a "primitive type"? You see, if you go into the "why" business; then why do you think that your idea is "correct"; in comparison to the existing model? – GhostCat Jun 19 '15 at 13:55
  • autoboxing is conversion from unboxed to boxed type. How you can define conversion from type that does not exist? ``Long l = 10;` doesn't work either because of type mismatch. – talex Jun 19 '15 at 13:55
  • @talex and Jägermeister: So it's really just because the conversion would fall out of the scope of the autoboxing definition? You could still do it, but call it something else... – mastov Jun 19 '15 at 13:57

5 Answers5

4

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.

Community
  • 1
  • 1
nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • @mastov It is because `BigInteger` isn't part of the Java language. It's part of the Java API. – nanofarad Jun 19 '15 at 13:47
  • Rolled back. This is a quote from the JLS, and the JLS does not say "From the void type to the Void type". It's also not possible to box or unbox since there can be no instances of the `Void` type. – nanofarad Jun 19 '15 at 13:49
  • @mastov No problem, no need to delete. – nanofarad Jun 19 '15 at 13:51
  • See [Void](https://docs.oracle.com/javase/7/docs/api/java/lang/Void.html) class for object as generic type for `void` – MaxZoom Jun 19 '15 at 13:55
  • @MaxZoom The `Void` class is not autoboxed, as there can be **no instances of it**. Additionally, I am referencing the JLS in this statement. Modifying the quote with a line about `void` is not proper since the JLS didn't write it. – nanofarad Jun 19 '15 at 13:56
2

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.

Community
  • 1
  • 1
Srini
  • 1,626
  • 2
  • 15
  • 25
0

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

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • That wouldn't cause a technical problem because conversion works really only one-way, so you could use `int` or `long` as primitive. So it's a conceptual thing, and not done for technical reasons? – mastov Jun 19 '15 at 13:56
0

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);

rossum
  • 15,344
  • 1
  • 24
  • 38
  • 1
    "You will see that it does not need autoboxing because it is already a class, not a primitive" `java.lang.Integer` and so on are also classes and not primitives. – nanofarad Jun 19 '15 at 13:50
  • `Integer` is a class; `int` is a primitive. Autoboxing converts an `int` to an `Integer`. Autoboxing only works from specific primitives to specific classes; it is not a universal converter between all primitives and all classes. If you want to convert between a primitive and a class where no autoboxing is provided, then you meed a specific method to do the work. In the case of `int` to `BigInteger` conversions, then use the `valueOf()` method. – rossum Jun 19 '15 at 13:58
  • Rossum: I know what autoboxing is. Your statement lexically wasn't a proper reasoning for it, however. – nanofarad Jun 19 '15 at 13:58
0

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?

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • The question was about "autoboxing", not "unboxing". But yes, maybe they don't want to do "autoboxing" for something they can't "unbox". Maybe. – mastov Jun 19 '15 at 14:02