Java Wrapper Classes

In this tutorial we will discuss details of wrapper classes like what wrapper classes is, list of wrapper classes, conversion from primitive to wrapper type and vice-versa, AutoBoxing and Unboxing, java program of primitive to wrapper conversion, why do we use wrapper classes etc.

Define wrapper class in Java

In java all primitive data types are value types which means the variable of primitive types holds values directly in memory. Java provides inbuilt classes corresponding to each primitive type that can be used to convert these value types in object types. These inbuilt classes are known as wrapper classes or primitive wrapper classes. The table below shows the list of all primitive data type and their corresponding wrapper class.

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

These classes are called wrapper classes since it wraps(encloses) around primitive types and converts them into object or non-primitive type. In addition, these classes also contains some useful methods which are often used in java programs.

Conversion from primitive to wrapper class

The code below shows how to convert a primitive int and double value to to their corresponding wrapper type. Similarly other primitive values can also be converted in to their respective wrapper type.

 int i = 100;
 // Converting into wrapper object
 Integer itr = new Integer(i); 
 // OR
 Integer itr = Integer.valueOf(i);
 // OR
 Integer itr = i;
 
 double d = 10.5;
 Double db = new Double(d);
 // OR
 Double db = Double.valueOf(d);
 // OR
 Double db = d;

After conversion we can use different methods of wrapper class with it's object. In above example we can call methods of Integer class with object itr.

Conversion from wrapper class to primitive

You can get back the original primitive value from the wrapper object using the wrapper class method or by directly assigning the wrapper object into corresponding primitive variable. Similar methods are available in other wrapper classes as well to get the original primitive value.

 Integer in = new Integer(100);
 // Converting into primitive type
 int i = in.intValue();
 // OR
 int i = in;
 
 Double db = new Double(10.5);
 double d = db.doubleValue();
 // OR
 double d = db;

Autoboxing and Unboxing in Java

Autoboxing : The automatic conversion of primitive type into an object of corresponding wrapper type is known as autoboxing. For example conversion of an int to an Integer or double to Double and so on. Autoboxing happens automatically, it's the compiler who does this for us.

Unboxing : The automatic conversion of wrapper type object into corresponding primitive type is known as unboxing. For example conversion of Integer to an int or Double to double and so on. It also happens automatically, done by java compiler.

 int i = 100;
 double d = 10.5;
 // Following are autoboxing
 Integer in = i;
 Integer ig = 20;
 Double db = d; 
 Double dl = 20.5; 
 // Following is not autoboxing
 // Integer in = new Integer(i);

 ----------------------------------------
 
 Integer in = new Integer(100);
 Double db = new Double(20.5);
 // Following are unboxing
 int i= in;
 double d = db;
 int j = new Integer(50);
 double k = new Double(50.5);
 // Following is not unboxing
 // int i = in.intValue();

Wrapper class program in java

 class WrapperClassExample {  
    public static void main(String args[]) {    
      int i = 50;    
      double d = 20.5;    
  
      // Converting primitives into objects  
      Integer intObj = new Integer(i);   
      Double doubleObj = new Double(d);   
      System.out.println("........ Printing wrapper objects ........ ");
      System.out.println("Integer object : "+intObj+", Double object : "+doubleObj);
      // Can use below as well for conversion 
      Integer intObj2 = Integer.valueOf(i);   
      Double doubleObj2 = Double.valueOf(d); 
      System.out.println("intObj2 : "+intObj2+", doubleObj2 : "+doubleObj2);
      // Autoboxing : Converting primitives into objects 
      Integer intObj3 = i;   
      Double doubleObj3 = d;   
      System.out.println("intObj3 : "+intObj3+", doubleObj3 : "+doubleObj3);

      // Converting the wrapper object to primitive
      int in = intObj.intValue();
      double db = doubleObj.doubleValue();
      System.out.println("........ Printing primitve values ....... ");
      System.out.println("int value : "+in+", double value : "+db);
      //Unboxing : Converting the wrapper object to primitive
      int in2 = intObj;
      double db2 = doubleObj;
      System.out.println("int value2 : "+in2+", double value2 : "+db2);      
    } 
 }  

Output:

........ Printing wrapper objects ........ 
Integer object : 50, Double object : 20.5
intObj2 : 50, doubleObj2 : 20.5
intObj3 : 50, doubleObj3 : 20.5
........ Printing primitve values ....... 
int value : 50, double value : 20.5
int value2 : 50, double value2 : 20.5

String to numeric conversion in Java

If a number is presented in string form, it can be converted to a numeric value using methods of wrapper class. For example string "100" can be converted into numeric value 100 using wrapper class method. Each of the wrapper classes (except Character class) has a method like parseXXX(String) which converts a numeric string into a numeric value. A numeric value can be converted into string using toString() method of wrapper classes.

The command line arguments are also passed as string. If it's a numeric string we can convert that string to numeric value using wrapper class methods. The program below shows the conversion of numeric string to numeric value and vice-versa.

 class StringConversion {  
   public static void main(String args[]) {    
     String str = "100";
     String str2 = "100.5";
     int i = Integer.parseInt(str);
     double d = Double.parseDouble(str2);
     System.out.println("int value: "+i+", double value: "+d);
      // Converting integer value to string
     String str3 = Integer.toString(i); 
     System.out.println("String value: "+str3);   
      // Converting command line argument
     if(args.length != 0) {
        int j = Integer.parseInt(args[0]);
        System.out.println("Command line integer value = "+j);
      }
   }
 }  

Let's execute below commands to compile and execute the above program.

javac StringConversion.java
java StringConversion 500

Here argument 500 will be passed as string to main method which get's stored in args[0]. This string is then converted to an integer value using parseInt method of Integer class.

Output:

int value: 100, double value: 100.5
String value: 100
Command line integer value = 500

What if I try to convert a non numeric string to numeric value using wrapper class methods ?

The program will throw NumberFormatException. For example if you run command java StringConversion hello for above program, it will throw the exception because a non numeric string can not be converted to an integer.

Use of wrapper classes in Java

List given below points some of the reasons to use the wrapper classes in java :

  • To convert the primitive data types to object or non-primitive types.
  • To convert the numeric strings into numeric values.
  • Data structures in collection framework such as ArrayList, LinkedList, HashMap etc works with object types only and not primitive types. Example :

     ArrayList<Integer>
     HashMap<Integer,String>
     ArrayList<int> // compilation error
    
  • Wrapper classes provide many ready to use utility methods, for example Character class provides method to convert a char value to upper/lower case, Integer class provides method to find min/max of two number etc.
  • Primitive data types are passed by value while objects are passed by reference to a method. Wrapper classes provides the ability to pass primitives types as reference type to a method.