+ 17
What is autoboxing and autounboxing in java?
What is autoboxing and autou boxing in java?Why are the used?
3 Answers
+ 17
Autoboxing and unboxing is a convenient way to auto transform primitive data type to itâs corresponding java wrapper classes and vice versa.
Autoboxing in Java:
Converting a primitive data type into an object of the corresponding wrapper class is called autoboxing. For example, converting int to Integer or converting long to Long object.
Unboxing in Java:
Converting an object of a wrapper type to its corresponding primitive data type is called unboxing.
+ 6
autoboxing is when the java compiler automatically converts a primitive type into the corresponding wrapper object.
for example,
- an int (eg 2) to an Integer.
- a character (ag 'a') to a Character
This allows to pass the primitive type to a function that expects the wrapper type as parameter, without having to do the conversion yourself, so the code is simpler to read.
+ 4
It is ralated to Wapper Classes in Java,
-Autobox
primitive â-> object
examples
charâ->Character
booleanâ-> Boolean
byteâ->Byte
shortâ->Short
intâ->Integer
longâ->Long
floatâ->Float
doubleâ->Double
Unboxing is revers of the above.