When Java automatically converts a
primitive type like int into corresponding wrapper class object e.g. Integer
than its called auto boxing because primitive is boxed into wrapper class while
in opposite case is called unboxing, where an Integer object is converted into
primitive int. All primitive types e.g. byte, short, char, int, long, float,
double and boolean has corresponding wrapper class e.g. Byte, Short, Integer, Character etc.
and participate in auto boxing and unboxing. Since whole process happens
automatically without writing any code for conversion it’s called auto boxing
and auto unboxing.
This
conversion or need of auto boxing comes into play when we use collection which
is used to store the object type then if we give the value of type int, byte
etc. then auto boxing automatically converts it into the corresponding wrapper
class object.
ArrayList
ar = new ArrayList();
Ar.add(1); //Auto Boxing
Ar.add(1); //Auto UnBoxing
int
number = Ar..get(0); // unboxing
No comments:
Post a Comment