Wrapper Class in Java – Autoboxing & Unboxing With Examples

1. Objective

In our last tutorial, we discussed the Java Comparator Interface. Now, in this tutorial for Java, we are going to learn how to use wrapper class in java. Moreover, we will discuss what is the Java wrapper class with an example, and its advantages. Along with this, we will cover the implementation of wrapper class inJava with Autoboxing and Unboxing in Java.

So, let’s start Wrapper Class in Java Programming.

Wrapper Class in Java - Autoboxing & Unboxing With Examples

Java Wrapper Class – Autoboxing & Unboxing With Examples

2. What is the Wrapper Class in Java?

A Wrapper class in Java is the type of class which contains or wraps the primitive data types when a wrapper class is created a field is created and in that field, we store the primitive data types.

3. Advantages of Wrapper Class in Java

Why we need for Wrapper Classes in Java Programming Language.

  • 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 wrapper class in Java 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 Types - Java Wrapper Class

Primitive Data Types – Java Wrapper Class

Do you know What is Java Inner Class?

4. Autoboxing and Unboxing in Java

a. Wrapper Class in Java – Autoboxing

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

Example –

// Java program to demonstrate Autoboxing
import java.util.ArrayList;
class Autoboxing
 {
    public static void main(String[] args)
      {
         char ch = 'a';;
         Character a = ch;
         ArrayList<Integer> arrayList = new ArrayList<Integer>()
         arrayList.add(25);
         System.out.println(arrayList.get(0));
     }
 }

b. Wrapper Class in Java – Unboxing

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

// Java program to demonstrate Unboxing
import java.util.ArrayList;
class Unboxing
 {
    public static void main(String[] args)
     {
         Character ch = 'a';
         char a = ch;
         ArrayList<Integer> arrayList = new ArrayList<Integer>();
         arrayList.add(24);
         int num = arrayList.get(0);
         System.out.println(num);
     }
 }

Explore more about Autoboxing and Unboxing in Java

5. Implementation of Wrapper class

Let’s see how can we implement Java wrapper class.

class WrappingUnwrapping
 {
    public static void main(String args[])
      {
//  byte data type
          byte a = 1;
// wrapping around Byte object
          Byte byteobj = new Byte(a);
// int data type
          int b = 10;
//wrapping around Integer object
          Integer intobj = new Integer(b);
// float data type
          float c = 18.6f;
// wrapping around Float object
          Float floatobj = new Float(c);
// double data type
          double d = 250.5;
// Wrapping around Double object
          Double doubleobj = new Double(d);
// char data type
          char e='a';
// wrapping around Character object
          Character charobj=e;
//  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 bv = byteobj;
          int iv = intobj;
          float fv = floatobj;
          double dv = doubleobj;
          char cv = charobj;
          System.out.println("Unwrapped values ");
          System.out.println("byte value, bv: " + bv);
          System.out.println("int value, iv: " + iv);
          System.out.println("float value, fv: " + fv);
          System.out.println("double value, dv: " + dv);
          System.out.println("char value, cv: " + cv);
     }
 }

Read about Interface in Java with examples

So, this was all about Java Wrapper Class. Hope you like our explanation.

6. Conclusion

Hence, in this Java Wrapper Class tutorial, we learned what is wrapper class in Java, usees of wrapper classes in java. In addition, we talked about autoboxing and unboxing in java, Wrapper class with examples to understand the implementation of Java Wrapper class. Furthermore, if you have any doubt, feel free to ask in the comment section.

Related Topic- Serialization and Deserialization in Java

For reference

Leave a Reply

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