Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is autoboxing possible for the classes I create?

Tags:

Is there any way to use autoboxing for the classes I create? For example, I have this subclass of Number.

public class UnsignedInteger extends Number {     int n;      public UnsignedInteger(int n) {         if(n >= 0)             this.n = n;         else             throw new IllegalArgumentException("Only positive integers are supported");     } } 

Now, UnsignedInteger i = new UnsignedInteger(88); works perfectly fine, but is there any way to make this compile : UnsignedInteger i = 88;? It won't for me. Thanks in advance!

like image 894
JavaNewbie_M107 Avatar asked Jul 12 '13 16:07

JavaNewbie_M107


People also ask

Is Autoboxing and unboxing same?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

How do you use Autoboxing?

For example, converting int to Integer class. The Java compiler applies autoboxing when a primitive value is: Passed as a parameter to a method that expects an object of the corresponding wrapper class. Assigned to a variable of the corresponding wrapper class.

What is the major advantage of Autoboxing?

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

Is Autoboxing also known as boxing?

The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing.


2 Answers

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.

like image 92
rgettman Avatar answered Jan 30 '23 20:01

rgettman


No, unfortunately. Automatic boxing conversions (as per JLS §5.1.7) are only defined for the standard primitive wrapper classes.

like image 44
arshajii Avatar answered Jan 30 '23 20:01

arshajii