Wrapper Class in Java – Implement Autoboxing & Unboxing with Examples

We all know Java is not a pure object oriented programming language, the main reason behind this is that it works on primitive data types. To use data types in the form of objects we use wrapper classes. Wrapper class in Java is mainly an object which makes the code fully object oriented. For example, int-Integer, char – Character, etc.

In this article, you will get to know how Autoboxing and Unboxing in Java work with an example. So, let start with the introduction.

Learn the implementation of Warpper Class in Java with an example

1. What is the Wrapper Class in Java?

A Wrapper class in Java is the type of class which contains or the primitive data types. When a wrapper class is created a new field is created and in that field, we store the primitive data types. It also provides the mechanism to covert primitive into object and object into primitive.

Working with collections, we use generally generic ArrayList<Integer> instead of this ArrayList<int>. An integer is wrapper class of int primitive type. We use a Java wrapper class because for generics we need objects, not the primitives.

Wait, have you studied Array Vs ArrayList in Java?

Advantages of Java Wrapper Class

  • They are used to convert the primitive data types into objects (Objects are needed when we need to pass an argument in the given method).
  • The package java.util contains classes which only handles objects, so it helps in this case too.
  • Data Structures store only objects and primitive data types.
  • In multithreading, we need an object to support synchronization.
Primitive Data TypeWrapper Class
charCharacter
byteByte
shortShort
longLong
doubleDouble
floatFloat
booleanBoolean
intInt

Let’s revise the concept of Primitive Data Types in Java.

2. Autoboxing and Unboxing in Java

2.1 Autoboxing in Java

Java Autoboxing is used to automatically convert the primitive data types into corresponding objects.

Example –

package com.dataflair.wrapperclass;
import java.util.ArrayList;
public class Autoboxing 
{
  public static void main(String[] args)
  {
    char character = 'a';
    Character a = character;
    ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(25);
    System.out.println(arrayList.get(0));
  }
}

Output-Autoboxing in java

2.1 Unboxing in Java

Java Unboxing is reverse of Autoboxing, i.e. it converts wrapper class object into its corresponding primitive data type.

Join DataFlair on Telegram

Example –

package com.dataflair.wrapperclass;
import java.util.ArrayList;
public class Unboxing {

  public static void main(String[] args)
  {
    Character character = 'a';
    char a = character;
    ArrayList<Integer> arrayList = new ArrayList<Integer>();
    arrayList.add(25);
    int num = arrayList.get(0);
    System.out.println(num);
  }
}

Output- 

Unboxing in Java

Let’s discuss the important points of Multithreading in Java.

3. How to Implement a Wrapper Class?

Let’s study the implementation with an example-

package com.dataflair.wrapperclass;
public class ImplementingWrapperClass {
  public static void main(String args[])
  {
    //  byte data type
    byte byteVar = 1;
    // wrapping around Byte object
    Byte byteobj = new Byte(byteVar);
    // int data type
    int intVar = 10;
    //wrapping around Integer object
    Integer intobj = new Integer(intVar);
    // float data type
    float floatVar = 18.6f;
    // wrapping around Float object
    Float floatobj = new Float(floatVar);
    // double data type
    double dobleVar = 250.5;
    // Wrapping around Double object
    Double doubleobj = new Double(dobleVar);
    // char data type
    char charVar='a';
    // wrapping around Character object
    Character charobj=charVar;
    //  printing the values from objects
    System.out.println("Values of Wrapper objects (printing as objects)");
    System.out.println("Byte object byteobj:  " + byteobj);
    System.out.println("Integer object intobj:  " + intobj);
    System.out.println("Float object floatobj:  " + floatobj);
    System.out.println("Double object doubleobj:  " + doubleobj);
    System.out.println("Character object charobj:  " + charobj);
    // objects to data types (retrieving data types from objects)
    // unwrapping objects to primitive data types
    byte unwrappingByte = byteobj;
    int unwrappingInt = intobj;
    float unwrappingFloat = floatobj;
    double unwrappingDouble = doubleobj;
    char unwrappingChar = charobj;
    System.out.println("Unwrapped values ");
    System.out.println("byte value, unwrapped Byte: " + unwrappingByte);
    System.out.println("int value, unwrapped Int: " + unwrappingInt);
    System.out.println("float value, unwrapped Float: " + unwrappingFloat);
    System.out.println("double value, unwrappied Double: " + unwrappingDouble);
    System.out.println("char value, unwrapped Char: " + unwrappingChar);
  }
}

Output-

Implementing-wrapper-class

Java Quiz

4. Summary

Today, we learned about what is wrapper class and how to use it in java. In addition, we talked about autoboxing and unboxing with example. At last, we implement the wrapper class with an example program.

If you have any doubt, feel free to ask in the comment section.

Next article in the Java DataFlair tutorial series – Collections in Java

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.