What is a Wrapper Class in Java?
Java is an object-oriented language that converts a primitive data type into a class object; hence, wrapper class objects enable us to convert the original passed value. These wrapper classes support the multithreading and synchronisation process. With the help of this, we can work with collections like Vector, LinkedList, ArrayList.
Process Flow of the Wrapper Class
In a wrapper class, we create the object with fields or properties, where we can use and store the primitive data types.
Java implements in-built classes corresponding to specific primitive types that can be applied to modify these value types in object types. We can consider and identify these inbuilt classes as wrapper classes or primitive wrapper classes.
Use of Wrapper Class in Java Programming
- We can use the wrapper class and create a function with parameters using primitive data types.
- We can change the primitive data type into a distinct wrapper class.
- Wrapper class allows various methods while using collections like sorting and searching.
- The wrapper class implements the technique to convert the primitive into object and object into primitive.
- There is a concept of autoboxing and unboxing in the wrapper class, which transform the primitives into objects and objects into primitives automatically.
- In Java, we have a synchronisation mechanism that works with objects in multithreading.
What are Primitive Data Types?
Before going into a deeper understanding of the wrapper class object, we need to understand the concept of primitive data types:
A primitive data type defines the size and type of variable values. In Java, we have eight primitive data types, which are as follows:
Features of Java Wrapper Classes
- Wrapper classes transmute numeric strings into numeric values.
- We can store primitive data into an object.
- We can use the valueOf() method in all the wrapper classes without string.
- We can use the typeValue() for all the available wrapper classes. It performs as the primitive type and returns the value of the object.
Below are the Features of the Wrapper Class:
- Modification of the value in function: In java programming, we have the ‘call by value’ function, with the help of which we can pass the primitive value parameter.
- Serialisation: In serialisation, we convert the object within streams to implement the serialisation. We can regenerate it in the object with the help of the classes of the wrapper.
- Synchronisation: Java synchronisation operates into objects in multi-threading.
- java.util package: The java.util package implements the utility classes to match with objects.
- Collection framework: Java collection framework operates including objects only. Here, we have many collection framework classes—for example—HashSet, TreeSet, ArrayDeque, ArrayList, Vector, and LinkedList.
Also Read: Java Architecture & Components
Use of Autoboxing and Unboxing in Wrapper Class with Examples
Autoboxing: It is the automated transformation of primitive types to the object of their corresponding wrapper classes—for example, conversion of long to Long and int to Integer, double to Double.
Unboxing in Java
Unboxing is the reverse of Autoboxing. It is the method of converting a Wrapper class object within its corresponding Primitive Data type. For example, we can convert an Integer object into a primitive data type, as int.
Benefits of Wrapper Classes
- Wrapper class provides various methods which we can use with collections like sorting, searching.
- We can use the objects of the wrapper class and store them as a null value.
- A wrapper type allows a primitive to operate in more advanced ways. An integer can use it in different ways, like a class described as Hours, for example, which presents the number meaning wherever it is used.
- The primitive types just operate value; the wrapper class provides it with a name.
- Wrapper objects are natural objects, and their references can be null. This enables the usage of a ‘not set’ state, which is difficult with primitives.
Checkout: Java Projects & Topics
We hope this article helped you with the understanding of the wrapper class in Java.
If you’re interested to learn more about Java, full-stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.