wrapper class in java

Let’s learn wrapper class in java.

wrapper class in java

wrapper classes allow primitive data types a way to convert into object and vice-versa.

For example:

int a = 7; // using primitive datatype

Integer a = new Integer(7); // using wrapper class

Since Java 1.0 wrapper class is been there and from Java version 5.0 auto boxing and unboxing allows converting primitive data types to objects and objects to primitive data types automatically. Let’s see wrapper class in java with example.

public class WrapperClassExample
{
   public static void main(String[] args)
   {
      // before java 5 version
      int num = 70;
      // we were converting explicitly like this
      Integer i = Integer.valueOf(num);
      // printing Integer object value explicitly
      System.out.println(i);

      // in java 5 version
      // Autoboxing and unboxing feature was introduced
      // now there is no need of explicit conversion
      Integer i1 = num;
      System.out.println(i1);
   }
}


Output:

70
70


Here is the list of 8 primitive datatypes with associated wrapper class. These wrapper classes are available in java.lang package.

primitive data typewrapper class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

Autoboxing in java

autoboxing in java means to convert primitive data type to its corresponding wrapper class. That is from int(primitive data type) to Integer(wrapper class), long(primitive data type) to Long(wrapper class) and etc.

Let’s see autoboxing example(primitive data type to wrapper class).

// wrapper class in java example
public class AutoboxingExample
{
   public static void main(String[] args)
   {
      // here we are converting int into Integer
      int num = 50;
      // explicitly converting int into Integer
      Integer a = Integer.valueOf(num);  
      // Autoboxing - internally compiler write Integer.valueOf(num) 
      Integer b = num;      
      System.out.println(num + " " + a + " " + b);
   }
}



Output:

50 50 50


Unboxing in java

unboxing means convertion of wrapper class into corresponding primitive data type. Wrapper to primitive data type example.

// wrapper class in java example
public class UnboxingExample
{
   public static void main(String[] args)
   {
      // here we are converting Integer to int
      Integer num = new Integer(70);    
      // explicitly converting Integer to int
      int a = num.intValue(); 
      // unboxing - internally compiler write num.intValue()
      int b = num;        
      System.out.println(num + " " + a + " " + b);
   }
}



Output:

70 70 70


wrapper classes methods in java

wrapper class methods are used to get corresponding value associated with wrapper object. Here are wrapper class methods intValue(), byteValue(), shortValue(), longValue(), floatValue(), doubleValue(), charValue(), booleanValue().

Let’s see wrapper class methods example.

public class WrapperClassMethodsExample
{
   public static void main(String[] args)
   {
      Double d = 7.7;
      Integer i = 50;
      Character ch = 'F';
      System.out.println(d.doubleValue());
      System.out.println(i.intValue());
      System.out.println(ch.charValue());
   }
}



Output:

7.7
50
F


Custom wrapper class

By now we know wrapper classes is used to wrap primitive data types. Let’s create custom wrapper class.

public class CustomWrapperExample
{
   private char ch;
   // parameterised constructor to perform autoboxing
   public CustomWrapperExample(char ch)
   {
      this.ch = ch;
   }
   // getter method to perform unboxing
   public char getCharacterValue()
   {
      return ch;
   }
   // setter method
   public void setCharacterValue(char ch)
   {
      this.ch = ch;
   }
   // override
   public String toString()
   {
      return Character.toString(ch);
   }
}
public class TestCustomWrapper
{
   public static void main(String[] args)
   {
      char c = 'F';
      CustomWrapperExample obj = new CustomWrapperExample(c);
      System.out.println("Autoboxing value: " + obj);
      char c1 = obj.getCharacterValue();
      System.out.println("Unboxing value: " + c1);
   }
}


Output:

Autoboxing value: F
Unboxing value: F


Use of wrapper class in java or why we use wrapper classes in java

  • primitive data types do not belong to any class and they are not objects. Hence use wrapper classes when there is need of objects.
  • Data structure or collection framework(ArrayList, HashSet, TreeSet, LinkedHashSet etc) works with only objects.
  • Generics allow using wrapper classes as type parameter. Click here for example.
  • To support synchronization we need objects.
  • Wrapper class objects allow null values.