Boxing and unboxing
Every type in Java is either a reference type or a primitive type. A reference type is any class, instance, or array type. All reference types are subtypes of class Object, and any variable of reference type may be set to the value null. There are eight primitive types, and each of these has a corresponding library class of reference type. The library classes are located in the package java.lang.
Primitive : Reference Mapping byte : Byte short : Short int : Integer long : Long float : Float double : Double bool : Boolean char : Character
Conversion of a primitive type to the corresponding reference type is called boxing and conversion of the reference type to the corresponding primitive type is called unboxing.
Autoboxing / unboxing is the automated under the covers conversion between primitive types and their equivalent object types. For example, the conversion between an int primitive and an Integer object or between a boolean primitive and a Boolean object. This was introduced in Java 5.
// Assigning primitive type to wrapper type : Boxing
Integer iWrapper = 10;
// Assigning object to primitive : Unboxing
public void intMethod(Integer iWrapper){
int iPrimitive = iWrapper;
}
Posted at 11:47PM Mar 13, 2008 by Manveen Kaur in General | Comments[0]