What is Wrapper Class in Java?

What is Wrapper Class in Java?

4 mins read3.1K Views Comment
clickHere
Updated on Oct 12, 2023 17:21 IST

This article will teach you what is wrapper class and how wrapper objects are created. It will also teach you the concept of autoboxing and unboxing in java.

2022_08_Wrapper-class-in-JAVA.jpg

Wrapper Class in Java helps to use primitive data types ( int , char, Boolean, float, etc..) as objects. This article will discuss the Java wrapper class, its features, and the need to teach the wrapper class in the Java program. We will also talk about autoboxing and Unboxing.

What is Wrapper Class in Java?

A wrapper class in Java is a class whose object contains or surrounds primitive data types. Primitive data types can be stored in a field created when an object to a wrapper class is created. In other words, we can create a wrapper class object out of a primitive value.

The auto-boxing and unpacking feature in J2SE 5.0 automatically transforms primitives into objects and objects into primitives. Auto-boxing is the automatic transformation of a primitive into an object, and auto-unboxing is the opposite. 

In Java, the eight classes that make up Java. lang packages are referred to as wrapper classes. Following is a list of the eight wrapper classes:

Must Check: Data Types in JAVA

Primitive Type  Wrapper Class 
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Method Overriding in Java
Method Overriding in Java
The below tutorial article covers method overriding in Java with examples. It also covers why we override the methods and how we can protect overridden if needed. Method overriding has...read more
Abstract Class in Java
Abstract Class in Java
The tutorial article below covers data abstraction using abstract class in Java with examples and explanations. 
Interface in Java Explained
Interface in Java Explained
The below Java Tutorial Article explains the interface in Java with examples. Moreover it covers multiple inheritance through interfaces using examples.

Also explore: What is Java? Everything a Beginner Needs to Know

Creating Wrapper Objects

Utilize the wrapper class instead of the primitive type to produce a wrapper object. The user only needs to print the object to obtain the value. Let us have a look at the Java program below:

 
public class Main { 
  public static void main(String[] args) { 
    Integer myInteger = 7; 
    Double myDouble = 5.69; 
    Character myCharacter = 'B'; 
    System.out.println(myInteger);
    System.out.println(myDouble);
    System.out.println(myCharacter);
  }
}
Copy code
The output of the program will be:

7
5.69
B

In the above program, the objects of the wrapper class are Integer, Character, and Double making the required conversions for the desired outcome. 

Need of Wrapper Class

Since Java is an object-oriented language used for programming, The user frequently needs to work with objects in Collections, Synchronisation, Serialisation, and many others. Let’s look at some of the situations where using wrapper classes is necessary.

  • To carry out the serialization, we must transform the objects into streams. If we have a primitive value, we can use the wrapper classes to turn it into an object.
  • Java can only support call with value. Therefore, if we pass a primitive value, the original value will not be changed. But if we turn a primitive value into an object, the original value will be altered.
  • Java synchronization utilizes multi-threaded objects.
  • Java’s collection framework only functions with objects. The collection framework’s classes (such as Vector, HashSet, LinkedHashSet, PriorityQueue, ArrayList, TreeSet, LinkedList, and ArrayDeque) all exclusively deal with objects.
  • The utility classes to interact with objects are provided by java.util package.
Final Keyword in Java
Final Keyword in Java
The Final Keyword in Java allows declaring a class, data members, and methods to unalterable and behaves differently when it is used with classes, methods, and variables. In this article,...read more
Access Modifiers in Java
Access Modifiers in Java
Access modifiers are keywords that define the accessibility of a class and its members. Access modifiers are used in Java to control the visibility (accessibility) of classes, interfaces, variables, methods,...read more
Jump Statements in Java
Jump Statements in Java
In this article, we’ll cover the Jump Statements in Java, such as break, continue and return, along with examples and explanations.

Autoboxing

Autoboxing is the automatic transformation of primitive types into the object of the matching wrapper classes. As an illustration, convert an int to an integer, short to Short, long to a Long, a double to a Double, etc.

Let us see the Java program below to understand autoboxing:

 
import java.util.ArrayList;
class WrapperUnboxing
{
    public static void main(String[] args)
    {
        Character ch = 'x';
        char x = ch;
        ArrayList arrayList = new ArrayList();
        arrayList.add(29);
        int num = arrayList.get(0);
            System.out.println(num);
    }
}
Copy code

The output of the Java code is:

29

In the above code, char ch = ‘x’ is converting the primitive to character conversion and autoboxing is carried out because ArrayList only stores the objects. Finally, the output is printed as the value from the object.

Unboxing

Unboxing is a process of automatically converting a wrapper type into its equivalent primitive type. It is autoboxing done the other way around. Since Java 5, we are no longer required to transform wrapper types into primitives using the intValue() method of wrapper classes. For example, conversion of integer to int, Double to double, etc. 

Let us see a Java program below to understand Unboxing:

 
import java.util.ArrayList;
class WrapperUnboxing
{
    public static void main(String[] args)
    {
        Character ch = 'x';
        char x = ch;
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(29);
        int num = arrayList.get(0);
            System.out.println(num);
    }
}
Copy code

The output of the Java code is:

29

In the above code, char x= ch is converting the character object to primitive conversion and int num method returns the integer object finally because of unboxing. Finally, the output is printed as the value of primitive data types.

Let us see an example to wrapper class in Java with the process of autoboxing and unboxing:

 
class WrapperClass
{
    public static void main(String args&#91;])
    {
    byte x = 2;
    Byte byteobj = new Byte(x);
    int y = 20;
    Integer intobj = new Integer(y);
    float z = 28.6f;
    Float floatobj = new Float(z);
    double d = 258.5;
    Double doubleobj = new Double(d);
    char e='a';
    Character charobj=e;
    System.out.println("The values of Wrapper objects are");
    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);
    
        byte bytev = byteobj;
    int intv = intobj;
    float floatv = floatobj;
    double doublev = doubleobj;
    char charv= charobj;
    System.out.println("The unwrapped values are");
    System.out.println("byte value: " + bytev);
    System.out.println("int value: " + intv);
    System.out.println("float value: " + floatv);
    System.out.println("double value: " + doublev);
    System.out.println("char value: " + charv);
    }
}
Copy code

The output of the Java program is:

The values of Wrapper objects are Byte object byteobj: 2
Integer object intobj: 20
Float object floatobj: 28.6
Double object doubleobj: 258.5
Character object charobj: a
The unwrapped values arebyte value: 2
int value: 20
float value: 28.6
double value: 258.5
char value: a

In the above code, we are demonstrating wrapping and unwrapping. The statement used here byte x = 2 basically is a byte data type that would be wrapped around the byte object as Byte byteobj = new Byte(x). Similarly, the process is done further for all the data types. After printing the values from the objects, the objects are converted to the data types which are called unwrapping objects to primitive data types. Hence, the outcome is printed as values from data types. 

Advantages of Wrapper Class

  • Objects rather than raw data types may be required in Java at times. Wrapper classes enable us to use primitive data types as objects in these circumstances.
  • Null values can be stored in wrapper objects.

Conclusion

In this article, we have discussed wrapper classes in java and the need to have a wrapper class. A class whose object contains or surrounds primitive data types is known as a wrapper class. We have also discussed the process of autoboxing and unboxing with wrapper classes along with the advantages of the wrapper class. 

Download this article as PDF to read offline

Download as PDF
clickHere
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments

We use cookies to improve your experience. By continuing to browse the site, you agree to our Privacy Policy and Cookie Policy.