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.
why is increment in reference of integer class object manipulates integer value?
Integer x=new Integer(10); x++;
Here java compiler will do the un-boxing
of the object, which converting the Integer
object into primitive int
. Then the increment operation will be performed. This is only defied for java primitives, the auto-boxing
is converting primitive to wrapper object and reverse of it is call un-boxing
. This is what is happening here.
The auto-boxing
or un-boxing
which is an automatic conversion, it is defined for java primitives only. So it can not be performed on other objects. Remember, the object are not just memory references like C or C++
that we can increment them.
Is converting int to a String counted as Autoboxing?
No. Autoboxing only applies to primitives -> wrappers. Since there is no String primitive (or wrapper), String has nothing to do with autoboxing.
Why do we use autoboxing and unboxing in Java?
Some context is required to fully understand the main reason behind this.
Primitives versus classes
Primitive variables in Java contain values (an integer, a double-precision floating point binary number, etc). Because these values may have different lengths, the variables containing them may also have different lengths (consider float
versus double
).
On the other hand, class variables contain references to instances. References are typically implemented as pointers (or something very similar to pointers) in many languages. These things typically have the same size, regardless of the sizes of the instances they refer to (Object
, String
, Integer
, etc).
This property of class variables makes the references they contain interchangeable (to an extent). This allows us to do what we call substitution: broadly speaking, to use an instance of a particular type as an instance of another, related type (use a String
as an Object
, for example).
Primitive variables aren't interchangeable in the same way, neither with each other, nor with Object
. The most obvious reason for this (but not the only reason) is their size difference. This makes primitive types inconvenient in this respect, but we still need them in the language (for reasons that mainly boil down to performance).
Generics and type erasure
Generic types are types with one or more type parameters (the exact number is called generic arity). For example, the generic type definition List<T>
has a type parameter T
, which can be Object
(producing a concrete type List<Object>
), String
(List<String>
), Integer
(List<Integer>
) and so on.
Generic types are a lot more complicated than non-generic ones. When they were introduced to Java (after its initial release), in order to avoid making radical changes to the JVM and possibly breaking compatibility with older binaries, the creators of Java decided to implement generic types in the least invasive way: all concrete types of List<T>
are, in fact, compiled to (the binary equivalent of) List<Object>
(for other types, the bound may be something other than Object
, but you get the point). Generic arity and type parameter information are lost in this process, which is why we call it type erasure.
Putting the two together
Now the problem is the combination of the above realities: if List<T>
becomes List<Object>
in all cases, then T
must always be a type that can be directly assigned to Object
. Anything else can't be allowed. Since, as we said before, int
, float
and double
aren't interchangeable with Object
, there can't be a List<int>
, List<float>
or List<double>
(unless a significantly more complicated implementation of generics existed in the JVM).
But Java offers types like Integer
, Float
and Double
which wrap these primitives in class instances, making them effectively substitutable as Object
, thus allowing generic types to indirectly work with the primitives as well (because you can have List<Integer>
, List<Float>
, List<Double>
and so on).
The process of creating an Integer
from an int
, a Float
from a float
and so on, is called boxing. The reverse is called unboxing. Because having to box primitives every time you want to use them as Object
is inconvenient, there are cases where the language does this automatically - that's called autoboxing.
What code does the compiler generate for autoboxing?
You can use the javap
tool to see for yourself. Compile the following code:
public class AutoboxingTest
{
public static void main(String []args)
{
Integer a = 3;
int b = a;
}
}
To compile and disassemble:
javac AutoboxingTest.java
javap -c AutoboxingTest
The output is:
Compiled from "AutoboxingTest.java"
public class AutoboxingTest extends java.lang.Object{
public AutoboxingTest();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_3
1: invokestatic #2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
4: astore_1
5: aload_1
6: invokevirtual #3; //Method java/lang/Integer.intValue:()I
9: istore_2
10: return
}
Thus, as you can see, autoboxing invokes the static method Integer.valueOf()
, and autounboxing invokes intValue()
on the given Integer
object. There's nothing else, really - it's just syntactic sugar.
Related Topics
Can't Make Jackson and Lombok Work Together
How to Mock Java.Time.Localdate.Now()
Jdbc Driver Throws "Resultset Closed" Exception on Empty Resultset
How to Rotate Jpeg Images Based on the Orientation Metadata
Formatting a String to a Currency Format in Jasper Report
Java: String Concat VS Stringbuilder - Optimised, So What Should I Do
How to Update Maven Repository in Eclipse
Service Layer and Controller: Who Takes Care of What
Multiple Axes on the Same Data
The Easiest Way to Transform Collection to Array
How to Create Variables at Runtime in Java
In Which Thread Do Completablefuture's Completion Handlers Execute
Should I Use a Separate Scriptengine and Compiledscript Instances Per Each Thread