Wrapper classes in java

Let’s learn wrapper classes in java.

Wrapper classes in java

Wrapper classes provides a way to convert primitive type into wrapper object and vice-versa.

For example:

int a = 7; // using primitive datatype

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


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 means automatic conversion from primitive to wrapper object by compiler is called autoboxing. That is from int(primitive data type) to Integer(wrapper class).

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

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


Auto Unboxing in java

Auto unboxing means compiler converts wrapper object to primitive type automatically is called auto unboxing. Wrapper to primitive data type 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.
  • Used to define several utility methods for primitives. For example: String str = Integer.toString(6);
  • To support synchronization we need objects.
  • Wrapper class objects allow null values.