Javatpoint Logo

pls tell me about boxing and unboxing

By: janu19*** On: Sun Mar 24 16:47:15 IST 2013     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
boxing and unboxing with some examples.....Up0Down

 
the boxing and unboxing has come into the java-5.

Boxing is known as the autoboxing.

"The automatic conversion of Primitive data types(int,float,char,double,short etc.) into a Wrapper class is known as the autoboxing or boxing.

And the reverse is that the unboxing.


Example-

class A
{
int a=3;

Integer aa=new Integer(a)//Boxing

}


here the int converts to the object through a wrapper class.



and the reverse is that...

class A
{
Integer aa=new Integer(4)

int aa=5;
}

here is a boxing.

-Amit Saxena
Image Created0Down

By: [email protected] On: Mon Mar 25 11:06:04 IST 2013 Question Reputation0 Answer Reputation-4 Belt Series Points0 -4User Image
Are You Satisfied :1Yes0No
 
Java 5 (and hence AspectJ 1.5) supports automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,...) in assignments and method and constructor invocations. This conversion is know as autoboxing.

Java 5 also supports automatic unboxing, where wrapper types are automatically converted into their primitive equivalents if needed for assignments or method or constructor invocations.

For example:

int i = 0;
i = new Integer(5); // auto-unboxing

Integer i2 = 5; // autoboxing




-------------------------------------------------------------


When Java automatically converts a primitive type like int into corresponding wrapper class object e.g. Integer than its called autoboxing 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 autoboxing and unboxing. Since whole process happens automatically without writing any code for conversion its called autoboxing and auto unboxing.
Image Created0Down

By: [email protected] On: Mon Mar 25 12:06:55 IST 2013 Question Reputation0 Answer Reputation392 Belt Series Points0 392User Image
Are You Satisfied :2Yes0No