Pages

Saturday 3 November 2012

Wrapper class - Introduced in JDK 1.5

The classes specifically meant for Primitive Datatypes.

boolean -> Boolean
char-> Character
byte->Byte
short->Short
int->Integer
long->Long
float->Float
double->Double

sometimes it is required to treat a primitive types into an Object type.
so to achieve this wrapper classes are included in Java which are available in java.lang package.

How to Wrap primitive into Object
int x =9; x is a primitive.
Integer y = new Integer(x); y is an Object

How to Unwrap Object into Primitive
int x = y.intValue();
now x is again a primitive.
similarly we have repective methods in each wrapper class to get primitive for it.

before 1.5 -
ArrayList l = new ArrayList();
l.add(new Integer(3));

1.5 onwards
ArrayList l = new ArrayList();
l.add(3);

The above concept is autoboxing...will see in next post...