In this tutorial, we will learn about the Java Wrapper class with the help of examples.
The wrapper classes in Java are used to change over primitive sorts (int, char, float, and so forth) into relating objects.
Each of the 8 primitive types has corresponding wrapper classes.
Primitive Type | Wrapper Class |
byte | Byte |
boolean | Boolean |
char | Character |
double | Double |
float | Float |
int | Integer |
long | Long |
short | Short |
In this article, you will learn-
Convert Primitive Type to Wrapper Objects
We can likewise use the valueOf() strategy to change over primitive types into comparing objects.
Example 1: Primitive Types to Wrapper Objects
class Main {
public static void main(String[] args) {
// create primitive types
int a = 5;
double b = 5.65;
//converts into wrapper objects
Integer aObj = Integer.valueOf(a);
Double bObj = Double.valueOf(b);
if(aObj instanceof Integer) {
System.out.println("An object of Integer is created.");
}
if(bObj instanceof Double) {
System.out.println("An object of Double is created.");
}
}
}
Output
An object of Integer is created.
An object of Double is created.
In the above example, we have used the valueOf() technique to change over the primitive sorts into objects.
Here, we have used the instanceof operator to check whether the produced objects are of Integer or Double sort or not.
Be that as it may, the Java compiler can straightforwardly change over the primitive sorts into relating objects. For instance,
int a = 5;
// converts into object
Integer aObj = a;
double b = 5.6;
// converts into object
Double bObj = b;
This process is known as auto-boxing. To learn more, visit Java autoboxing and unboxing.
Note: We can likewise change over primitive types into wrapper objects using Wrapper class constructors. Yet, the use of constructors is discarded of after Java 9.
Wrapping Objects into Primitive Types
To change over objects into the primitive sorts, we can use the comparing value strategies (intValue(), doubleValue(), and so on) present in every wrapper class.
Example 2: Wrapper Objects into Primitive Types
class Main {
public static void main(String[] args) {
// creates objects of wrapper class
Integer aObj = Integer.valueOf(23);
Double bObj = Double.valueOf(5.55);
// converts into primitive types
int a = aObj.intValue();
double b = bObj.doubleValue();
System.out.println("The value of a: " + a);
System.out.println("The value of b: " + b);
}
}
Output
The value of a: 23
The value of b: 5.55
In the above example, we have used the intValue() and doubleValue() strategy to change over the Integer and Double items into relating primitive sorts.
In any case, the Java compiler can consequently change over objects into comparing primitive sorts. For instance,
Integer aObj = Integer.valueOf(2);
// converts into int type
int a = aObj;
Double bObj = Double.valueOf(5.55);
// converts into double type
double b = bObj;
This process is known as unboxing. To learn more, visit Java autoboxing and unboxing.
Advantages of Wrapper Classes
In Java, in some cases we may have to use objects rather than primitive data types. For instance, while working with assortments.
// error
ArrayList<int> list = new ArrayList<>();
// runs perfectly
ArrayList<Integer> list = new ArrayList<>();
In such cases, wrapper classes help us with use primitive data types as objects.
We can store the null value in wrapper objects. For example,
// generates an error
int a = null;
// runs perfectly
Integer a = null;
Note: Primitive sorts are more productive than comparing objects. Subsequently, when productivity is the prerequisite, it is constantly suggested primitive sorts.
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.