Java Wrapper Classes

Question : What are Wrapper Classes? Describe the wrapper classes in Java ?
Answer : 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.

Following table lists the primitive types and the corresponding wrapper classes:

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

 

     
Question : Creating Wrapper Objects with the new Operator ?
Answer : 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:
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’)

     
Question : Tell me about AutoBoxing and UnBoxing in Java 5.0?
Answer :

Boxing/Unboxing
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.
Let’s take a small example of incrementing the value of Integer Object for understanding the concept of Boxing/Unboxing.

Before Java 5

int intPrimitive = intObject.intValue();
intPrimitive++;
intObject = new Integer(intPrimitive);

Using Java 5

Integer intObject = new Integer(10);
intObject++;

The output for the above examples will be same. In the first example we are creating an Integer object, unwrapping it to primitive value, incrementing the primitive value, rewrapping it to Integer object again.

In the second example when the compiler encounters the line intObject++ compiler unwraps Integer object to primitive type, increments it and rewraps to Integer Object again,So When the compiler got to the line intObject++; it substitues the code which will be something like this:

Integer.valueOf(intObject.intValue()+1);

or

int intPrimitive = intObejct.intValue();
intPrimitive++;
intObject = new Integer(intPrimitive);

Below example’s show different ways of Boxing/Unboxing.

Boolean isValid = false; // Boxing

Short shortObject = 200; // Boxing

if(shortObject<20){ // unboxing
}

ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < 10; i++){
list.add(i); // Boxing
}

     

Leave a Reply