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?
First of all, note that
BigIntegeris part ofjava.mathand notjava.lang, and so would not receive special treatment by the language. All of the boxed types are injava.langand 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:
Source
However, there is a request to allow autoboxing
BigIntegerand giving special meaning to various mathematical operators when applied toBigIntegerobjects.