WRAPPER CLASS

Java is an object-oriented language and as said everything in java is an object. But what about the primitives? They are sort of left out in the world of objects, that is, they cannot participate in the object activities, such as being returned from a method as an object, and being added to a Collection of objects, etc.. As a solution to this problem, Java allows you to include the primitives in the family of objects by using what are called wrapper classes.

There is a wrapper class for every primitive data type in Java. This class encapsulates a single value for the primitive data type. For instance the wrapper class for int is Integer, for float is Float, and so on. Remember that the primitive name is simply the lowercase name of the wrapper except for char, which maps to Character, and int, which maps to Integer.

The wrapper classes in the Java API serve two primary purposes:
To provide a mechanism to “wrap” primitive values in an object so that the primitives can be included in activities reserved for objects, like as being added to Collections, or returned from a method with an object return value.
To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.
The wrapper object of a wrapper class can be created in one of two ways: by instantiating the wrapper class with the new operator or by invoking a static method on the wrapper class. We will explore this further in this article.
Before we can instantiate a wrapper class, we need to know its name and the arguments its constructor accepts. The name of the wrapper class corresponding to each primitive data type, along with the arguments its constructor accepts, is listed below:
 
Creating Wrapper Objects with the new Operator
Before we can instantiate a wrapper class, we need to know its name and the arguments its constructor accepts. The name of the wrapper class corresponding to each primitive data type, along with the arguments its constructor accepts, is listed below
 

Primitive datatype

Wrapper Class

Constructor arguments

boolean

Boolean boolean or String

byte

Byte byte or String

char

Character char

short

Short short or String

int

Integer int or String

long

Long long or String

float

Float float double or String

double

Double double or String

All the wrapper classes are declared final. That means you cannot derive a subclass from any of them. All the wrapper classes except Boolean and Character are subclasses of an abstract class called Number, whereas Boolean and Character are derived directly from the Object class. All of the wrapper classes except Character provide two constructors: one that takes a primitive of the type being constructed, and one that takes a String representation of the type being constructed

for example,

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Boolean  wboo = new Boolean("false");
Boolean  yboo=new Boolean(false);
Byte  wbyte = new Byte("2");
Byte  ybyte=new Byte(2);
Short  wshort = new Short("4");
Short  yshort = new Short(4);
Integer  wint = new Integer("16");
Integer  yint = new Integer(16);
Long  wlong = new Long("123");
Long  ylong = new Long(123);
Float  wfloat = new Float("12.34f");
Float  yfloat = new Float(12.34f);
Double  wdouble = new Double("12.56d");
Double  wdouble = new Double(12.56d);
Character  c1 = new Character('c');

The value may also be passed as a variable, as shown in the following example:

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
boolean  boo = false;
Boolean  wboo = new Boolean(boo);
byte  b = 2;
Byte  wbyte = new Byte(b);
short  s = 4;
Short  wshort = new Short(s);
int  i = 16;
Integer  wint = new Integer(i);
long  l = 123;
Long  wlong = new Long(l);
float  f = 12.34f;
Float  wfloat = new Float(f);
double  d = 12.56d;
Double  wdouble = new Double(d);

Note: There is no way to modify a wrapped value that is, the wrapped values are immutable. To wrap another value, you need to create another object.

Wrapping Primitives Using a static Method

All wrapper classes offers static value Of() methods which give you another approach to creating wrapper objects. Because it's a static method, it can be invoked directly on the class (without instantiating it), and will return the corresponding object that is wrapping what you passed in as an argument.

Both methods take a String representation of the appropriate type of primitive as their first argument, the second method (when provided) takes an additional argument, int radix, which indicates in what base (for example binary, octal, or hexadecimal) the first argument is represented.

For example,

Code:

1
2
3
Integer  i2 = Integer.valueOf("101011", 2);
// converts 101011 to 43 and assigns the
// value 43 to the Integer object i2

Or

Code:

1
2
f2 = Float.valueOf("3.14f"); 
// assigns 3.14 to the Float object f2