-
What is
autoboxing
?Autoboxing is a mechanism for implicit initialization of objects of wrapper classes (Byte, Short, Character, Integer, Long, Float, Double) with the values of their respective original primitive types (resp. , , , , , , ),
byte
withoutshort
explicitlychar
usingint
thelong
classfloat
constructordouble
.Autoboxing occurs when a primitive is directly assigned to a wrapper class (using the "
=
" operator), or when a primitive is passed to the method parameters (of the "wrapper class" type). Both variables of primitive types and compile-time constants (literals andfinal
-primitives) can be autoboxed into "wrapper classes". In this case, literals must be syntactically correct to initialize a variable of the original primitive type.Autoboxing variables of primitive types requires an exact match between the type of the original primitive and the type of the "wrapper class". For example, attempting to autobox a variable of type
byte
toShort
, without first explicitly casting it,byte->short
will cause a compilation error.The autoboxing of primitive type constants allows for wider matching bounds. In this case, the compiler is able to implicitly expand/narrow the type of primitives beforehand. The transformation takes place in two stages:
-
implicit extension (narrowing) of the original type of the primitive to the type of the primitive corresponding to the wrapper class (to convert
int->Byte
, first the compiler implicitly narrowsint
tobyte
) -
autoboxing the primitive into the appropriate "wrapper class" (the compiler autoboxes
byte->Byte
). however, there are two additional restrictions in this case: -
assigning a primitive to a "wrapper" can only be done with the "
=
" operator (you cannot pass such a primitive to the method parameters without explicit type casting) -
the type of the left operand must not be older than
Character
, the type of the right operand must not be older thanint
, (expansion/narrowingbyte <-> short
,byte <-> char
,short <-> char
and only narrowingbyte <- int
,short <- int
,char <- int
, all other options require explicit type casting)
An additional feature of the integer "wrapper classes" generated by autoboxing constants in the range -128 +127 is that they are cached by the JVM. Therefore, such wrappers with the same values will be references to one object.
-
-
Why is it used
autoboxing
?I will quote the lecture:
As far as you remember, Java has both types inherited from the class
Object
and primitive types. But, as it turned out, such a convenient thing as collections and generics can only work with types inherited fromObject
. -
Alternatives
autoboxing
?Didn't find an answer, but posted on StackOverFlow .
Based on this discussion, it appears that the alternative
autoboxing
to `y is to use primitive types, since usingautoboxing
`a reduces performance. Conclusion: useautoboxing
only where necessary.Article written about
Autoboxing
: Autoboxing: Traps and Advantages -
Wrapper types for primitive types
mutable
orimmutable
?Immutable
, since primitive objects are alsoimmutable
. To work as aMutable
type there is a classMutableInteger
, etc. -
How are primitive types converted to non-primitive counterparts?
This and the following questions are well answered by this article: Autoboxing and Unpacking in Java
This is the conclusion from it: autoboxing is a mechanism for implicitly converting primitive data types into appropriate wrapper classes (objects). The compiler uses a method
valueOf()
to convert primitive types to objects, and methodsIntValue()
,doubleValue()
etc. to get primitive types from an object (that is, reverse conversion). Autoboxing converts boolean typeboolean
toBoolean
,byte
toByte
,char
toCharacter
,float
toFloat
,int
toInteger
,long
toLong
,short
toShort
. Unpacking occurs in the opposite direction. -
How are non-primitive types cast to primitive?
Answered above.
-
How are primitive and non-primitive types compared?
This is discussed in detail in the lecture, but I found it, so to speak, the same thing, but in other words.
There are two ways in Java to compare objects for equality,
==
and theequals()
.==
used for primitive types. For "==
" objects, this is purely reference comparison. For other cases, you need to use theequals()
. Moreover, the methodhashCode()
serves (in theory) for the same purpose. It is considered good manners if you overrideequals()
andhashCode()
. After initialization of some objects a and b , the following rule must be fulfilled:If the expression
a.equals(b)
returns true , then ita.hashCode()
must be equal tob.hashCode()
. -
Does the operation always create a new object
autoboxing
?It's in the lectures:
When we assign a value of type Integer
int
, the method is calledInteger.valueOf
: the functionvalueOf
does not always create a new object of type Integer. It caches values from -128 to 127.If the passed value goes beyond these limits, then a new object is created, and if not, then no.
If we write
new Integer()
, then a new object is guaranteed to be created. If we callInteger.valueOf()
, explicitly or withautoboxing
, then this method can return for us both a new object and give an object from the cache if the passed number lies in the range from -128 to 127. -
How does on-operation caching work
autoboxing
?Answered in the question above, just in case I created a question on StackOverFlow , but they answer the same thing
-
What types and/or values does caching work for?
In the eighth question. If anyone has any thoughts on the last three questions, then write in the comments.
DefNeo
Level 36
GO TO FULL VERSION