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
Learn Software Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
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 Executive PG Program 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.
What are wrapper classes in Java?
In the Java language, a wrapper class is an interface which is used to pass a primitive type to the object-oriented component. For example, if we want to pass int value to the object class then we have to create a wrapper class which is written like Integer and after that we can use this wrapper class as an object and pass integer values to the object class and can do manipulations on primitive values. In Java, a wrapper class is a class that wraps one or more primitive data types. Wrapper classes are very common in java. For example, String, Integer and Boolean are wrapper classes. Wrapper classes are used to treat primitive data types as objects.
What are the advantages of wrapper classes in Java?
With wrapper classes, developers get the best of both worlds. Developers can use primitive values directly from the wrapper, or access the corresponding wrapper object methods. This means you can use the wrapper class to represent a primitive value (like a float) but the float is automatically created, read and written like a normal wrapper object (like a Double). This is much safer than storing primitives in a wrapper.
What is autoboxing and unboxing in Java?
Autoboxing and unboxing refers to situations where primitive values are automatically wrapped into their corresponding wrapper classes. Suppose if you have a value 5 and you assign that value to a variable of type Integer, the compiler will automatically box that value and will create a wrapper instance. This wrapper instance is an Integer class, rather than an int. Similarly, if you assign this Integer wrapper to a variable of type Double, the compiler will automatically unbox it, and it will wrap the value 5 into a primitive value of type double, and assign it to the variable of type double. Unboxing works in a similar manner. Suppose, you have a reference of an Integer class, which is an object, and you assign that object to a variable of primitive type int, the compiler will actually unbox the object and create a primitive int object. This is called primitive unboxing.