As far as I know, Integer is a wrapper class for int. So an object must be created for initializing value of a.
Integer x=new Integer(10);
But Integer x=10;
seems to work perfectly. Can someone please explain how?
As far as I know, Integer is a wrapper class for int. So an object must be created for initializing value of a.
Integer x=new Integer(10);
But Integer x=10;
seems to work perfectly. Can someone please explain how?
Thanks to 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.
See docs https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
Autoboxing freely converts between primitives and their wrapper classes in most contexts and is described in JLS, §5.1.7. Boxing Conversion.
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
int
to typeInteger
At run time, boxing conversion proceeds as follows:
- If
p
is a value of typeint
, then boxing conversion convertsp
into a referencer
of class and typeInteger
, such thatr.intValue() == p
[...]
If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.
Boxing is the process of wrapping a primitive value as an object of a class of the equivalent type. Unboxing refers to the opposite direction, going from object to primitive.
For example, an int
primitive can be boxed as a Integer
.
Auto-boxing is the Java compiler doing this wrapping/unwrapping behind the scenes.
Auto-boxing simplifies life for us app programmers. In our daily work we can often think of the primitive and object as being the same. But know that they are not the same. There are some situations where a programmer must be aware of whether a primitive or an object is in hand. Enjoy the convenience of auto-boxing, but do not operate blindly.
In your example:
Integer x = 10 ;
…the 10
is parsed by the compiler as a int
primitive. Then, via auto-boxing, that primitive is wrapped as an Integer
object. The instantiation of the Integer
object is happening behind the scenes, transparently. A reference to that object is then assigned to the variable named x
.
See the links above to Wikipedia. And see the tutorial by Oracle.
Understand that Java is not purely object-oriented. If it were, we would have no primitives; we would have only objects. And therefore we would have no boxing.
So why did the designers of Java include primitives, adding this wrinkle of complexity to our lives? To facilitate porting code from C and other C-like languages. Easy porting was a crucial requirement at the time of Java’s invention.