Is autoboxing possible for the classes I create?

In short, no. There’s no way to get that to compile.

Java only defines a limited set of pre-defined boxing conversions.

From the JLS, section 5.1.7:

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

Additionally, one might think of overloading the = operator to perform this conversion, but operator overloading is not supported in Java, unlike in C++, where this would be possible.

So your conversion is not possible in Java.

Leave a Comment