Java has a way to convert a primitive into an object and an object into a primitive. It is possible through a wrapper class in Java. Thus, a class in Java is a wrapper if its object contains or wraps primitive data types. Additionally, a wrapper class object that you build has a field where you can store primitive data types.
The blog is all about Java wrapper classes including examples, the need for a wrapper class, advantages, and uses.
You require the Wrapper class in Java under below conditions:
There are eight wrapper classes:
Primitive Data type: char
Wrapper class: Char
Primitive Data type: double
Wrapper class: Double
Conversion method: public static double parseDouble(String)
Primitive Data type: float
Wrapper class: Float
Conversion method: public static float parseFloat(String)
Primitive Data type: int
Wrapper class: Integer
Conversion method: public static int parseInt(String)
Primitive Data type: long
Wrapper class: Long
Conversion method: public static long parseLong(String)
Primitive Data type: short
Wrapper class: Short
Conversion method: public static short parseShort(String)
Primitive Data type: boolean
Wrapper class: Boolean
Conversion method: public static boolean parseBoolean(String)
Primitive Data type: byte
Wrapper class: Byte
Conversion method: public static byte parseByte(String)
class Main {
public static void main(String[] args) {
// creating objects of the wrapper class
Integer xOyj = Integer.valueOf(35);
Double yOyj = Double.valueOf(7.75);
// converting into primitive types
int a = xOyj.intValue();
double y = yOyj.doubleValue();
System.out.println("The value of x: " + x);
System.out.println("The value of y: " + y);
}
}
Output
The value of x: 35
The value of y: 7.75
class Main {
public static void main(String[] args) {
// creating primitive types
int x = 5;
double y = 5.65;
//converts into wrapper objects
Integer xOyj = Integer.valueOf(x);
Double yOyj = Double.valueOf(y);
if(aObj instanceof Integer) {
System.out.println("Created an object of Integer.");
}
if(bObj instanceof Double) {
System.out.println(" Created an object of Double.");
}
}
}
Output
Created an object of Integer.
Created an object of Double.
Unboxing is a way of transforming a wrapper type into an equivalent primitive type. Thus, it is autoboxing done the other way around. Since Java 5, there is no need to transform wrapper types into primitives using the intValue() method of wrapper classes.
Autoboxing is a way of automatically transforming primitive data types into their respective wrapper classes. Thus, it can be from:
Since Java 5, there is no longer a need to transform primitives into objects using the wrapper classes' valueOf() method.
An object is immutable when its state doesn't change after initialization.
Hence, programs must create a new object for each state change as you cannot change immutable objects. However, Immutable objects also have the following advantages:
Follow the below guidelines for creating an immutable class in Java:
We uncovered Java wrapper classes in this guide. Now, you know how to use a wrapper class to convert primitive types to objects. You are also aware of when to use objects over primitive types. In a nutshell, Java uses wrapper classes to turn primitive types (such as int, char, and float) into corresponding objects. For more technical updates, stay in touch with The IoT Academy!
Ans.The class Void is regarded as a wrapper. However, it does not wrap any values.
Ans.In Java, an immutable class prevents content changes once an object is created. Additionally, in Java, all the wrapper classes and String classes are immutable. You can also create your immutable class.
About The Author:
Digital Marketing Course
₹ 9,999/-Included 18% GST
Buy Course₹ 29,999/-Included 18% GST
Buy Course