Wrapper Class In Java: Welcome to another new post of core java tutorial series, in this post, we are going to discuss the wrapper class in Java. A wrapper class is a class whose objects are contained primitive data types. In simple word, When we are creating a wrapper class object, those object contains fields, and in the fields, we can store the primitive data types.
Java is an object-oriented programming language and can view everything as an object. If you saw in Java, a simple file can be treated as an object with the help of java.io.File, An address of a system can be seen as an object with the help of java.util.URL, an image also treated as an object with the help of java.awt.Image. Similarly, a simple primitive data type also can be converted into an object with the help of wrapper classes.
The primitive data types like boolean, char, byte, short, int, long, float & double are not the objects. These are defined in the Java programming language itself. That’s why sometimes, as per the requirement or for use in a better way, we need to convert them (Data type) into an object.
What are the Wrapper classes?
As the name suggests, a wrapper class wraps (encloses) around a data type and gives it an object appearance whenever a data type is required an object. So that object can be used as per the requirement.
Wrapper class also has some method that can help to unwrap the object and give back the data type. It is something similar to when you are ordering something, but you are collecting your product inside some cover to avoid the external sources, and when you got the product, you remove the cover and use it as per your requirement.
List of Wrapper classes
To Wrap or convert each primitive data type to object, there is a wrapper class. Eight wrapper classes are present inside the java.lang package. Which helps us to convert the following eight primitive data types into the respective object. Here below is the complete list of the primitive data types and the respective wrapper classes.
Primitive Type and The Respective Wrapper Class:
|Primitive Data Type||Wrapper class|
The hierarchy structure of those wrapper class looks like below:
[Add this pic]
All the eight wrapper classes are placed in java.lang package so that they are implicitly imported and made available to the programmer. As you can observe in the above hierarchy, the superclass of all numeric wrapper classes is Number, and the superclass for Character and Boolean is Object. All the wrapper classes are defined as final, and thus, designers prevented them from inheritance.
Importance of Wrapper classes
In Interview, you may face a common java interview questions, or sometimes you might think about “What is the purpose of a wrapper class?”.
- One of the main benefit of wrapper class it converts primitive data types into objects.
- The classes are defined inside Java.util package handles only objects, so that time wrapper class plays a significant role.
- When you are dealing with Hashmap, ArrayList, and Vector, these are stores only objects and non-primitive data types
As I have mentioned above, one of the main reason of using wrapper class is to use in the collection. and the other thing is wrapper objects store more memory as compared to the primitive data types, so as per your requirement, use wrapper class or primitive data types. If you need efficiency, then use primitive data type, and when you want objects at that time, use wrapper class objects.
To Deal with wrapper class and primitive data type in Java 1.5, a new feature is introduced, and that’s called Autoboxing and Unboxing. You can read the complete post about Autoboxing and Unboxing In Java using the link.
- Autoboxing: Automatic conversion of primitive data types into the wrapper class object is called Autoboxing. For example, int to Integer.
- Unboxing: Conversion of Wrapper class object to primitive data type to primitive data type is called unboxing.