Wrapper Class In Java

Wrapper Class In Java: Welcome to another new post of the core Java tutorial series; this post will discuss the wrapper class in Java. A wrapper class is a class whose objects are contained primitive data types. In simple words, When creating a wrapper class object, that 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 is 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 for an object. So that object can be used as per the requirement.

The wrapper class also has some methods to help unwrap the object and back the data type. It is similar to when you are ordering something, but you are collecting your product inside some cover to avoid external sources, and when you get 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 an object, there is a wrapper class. Eight wrapper classes are present inside the java.lang package. This helps us convert the following eight primitive data types into the respective objects. Below is the complete list of the primitive data types and the respective wrapper classes.

Primitive Type and The Respective Wrapper Class:

Primitive Data TypeWrapper class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

The hierarchy structure of those wrapper classes looks like the below:

Wrapper Type Class Hierarchy

All 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; thus, designers prevented them from inheritance.

Importance of Wrapper classes

In the Interview, you may face common Java interview questions, or sometimes you might think about “What is the purpose of a wrapper class?”.

  • One of the main benefits of the wrapper class is that it converts primitive data types into objects.
  • The classes are defined inside Java.util package handles only objects, so the time wrapper class plays a significant role.
  • When dealing with Hashmap, ArrayList, and Vector, these are store only objects and non-primitive data types.

As I have mentioned above, one of the main reasons for using the wrapper class is to use it in the collection. the other thing is wrapper objects store more memory than the primitive data types, so as per your requirement, use wrapper class or primitive data types. If you need efficiency, 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: Autoboxing and Unboxing. The link lets you read the complete post about Autoboxing and Unboxing In Java.

  • Autoboxing: The automatic conversion of primitive data types into the wrapper class object is called Autoboxing—for example, int to Integer.
  • Unboxing: The conversion of the Wrapper class object to a primitive data type is called unboxing.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment