Wrapper class, Auto boxing and Unboxing


  • Java 1.5 introduced a special feature of the auto conversion of primitive types to the corresponding Wrapper class and vice versa.

Wrapper Class

  • A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types. 
  • In other words, we can wrap a primitive value into a wrapper class object.
  • Java uses primitive data types such as int, double, float etc. to hold the basic data types.
  • Example:
  • To handle such type of situations, Java provides type Wrappers which provide classes that encapsulate a primitive type within an object.
    • Character: It encapsulates primitive type char within object.
    • Example:Character (char ch)
    • Boolean: It encapsulates primitive type boolean within object.
    • Boolean (boolean boolValue)
    • Numeric type wrappers: It is the most commonly used type wrapper.
    • Byte Short Integer Long Float Double


Need of Wrapper Classes

  • They convert primitive data types into objects. 
  • Objects are needed if we wish to modify the arguments passed into a method.
  • The classes in java.util package handles only objects and hence wrapper classes help in this case also.
  • Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
  • An object is needed to support synchronization in multithreading.

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

Autoboxing and Auto-unboxing


  • Autoboxing: Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. 
    • For example – conversion of int to Integer, long to Long, double to Double etc.


  • Auto-Unboxing: It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. 
    • For example – conversion of Integer to int, Long to long, Double to double etc.
  • Full Example:

class Test
{
 public static void main(String[] args)
 {
  Integer iob = 100; //Auto-boxing
  int i = iob;   //Auto-unboxing
  System.out.println(i+" "+iob);

  Character cob = 'a';  //Auto-boxing
  char ch = cob;  //Auto-unboxing
  System.out.println(cob+" "+ch);
 }
}

Benefits of Autoboxing / Unboxing

  • Autoboxing / Unboxing lets us use primitive types and Wrapper class objects interchangeably.
  • We don't have to perform Explicit typecasting.
  • It helps prevent errors.
  • Auto-unboxing also allows you to mix different types of numeric objects in an expression. When the values are unboxed, the standard type conversions can be applied.
  • Full Example:

class Test {
public static void main(String args[]) {
Integer i = 35;
Double d = 33.3;
d = d + i;
System.out.println("Value of d is " + d);
}
}

What happens behind the scenes?

  • In the above section we learnt how java compiler performs automatic conversion between primitive type and corresponding Wrapper objects. 
  • Lets discuss what compiler actually does during autoboxing and unboxing. 
  • The best way to understand this is to compare things before java 1.5 and after java 1.5 (boxing and unboxing introduced in java 1.5).
  • Autoboxing:

Integer number = 100;

  • What compiler does (or what we used to do before java 1.5):

Integer number = Integer.valueOf(100);

  • Unboxing:
  • What we see:

Integer num2 = new Integer(50);
int inum = num2;

  • What compiler does:

Integer num2 = new Integer(50);
int inum = num2.intValue();

  • Similar things happen with the other wrapper classes and primitive types such as long, double, short etc.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form