Friday, 22 April 2011

Primary datatypes and their wrapper classes

Corresponding to all the primitive type there is a wrapper class defined. These wrapper classes are OOP version of the primitive types. These classes provide useful methods for manipulating primitive data values and objects.

The language designers decided that the higher processing speed and memory efficiency of simple, non-class structures for such heavily used data types overruled the elegance of a pure object-only language.The designers decided instead that for each primitive type there would be a corresponding wrapper class. An instance of a wrapper contains, or wraps, a primitive value of the corresponding type.

Some primitive types and their wrapper classes:

Data types Wrapper class
int Integer
short Short
long Long
byte Byte
char Character
float Float
double Double

he wrapper constuctors create class objects from the primitive types. For example, for a double floating point number "d" :
     double d = 5.0;
     Double aD = new Double(d);

Here a Double wrapper object is created by passing the double value in the Double constructor argument.
In turn, each wrapper provides a method to return the primitive value
     double r = aD.doubleValue();

Each wrapper has a similar method to access the primitive value: integerValue() for Integer, booleanValue() for Boolean, and so forth.

No comments:

Post a Comment