Wrapper Classes in Java

In this tutorial, I am going to explain what are wrapper classes in java. Why it is introduced and the concept of autoboxing and unboxing.

Wrapper Class

As the name suggests, a wrapper class wraps (encloses) around a primitive data type and gives it an object appearance. In simple words, we can convert primitive data types into an object using their corresponding wrapper class.

Wrapper classes are final and immutable.

How to create immutable class in java

For example:

Primitive Data Type

A primitive data type defines the size and type of variable values. In Java, we have eight primitive data types, which are as follows:

primitive types in java

Now each primitive data type has its corresponding wrapper classes. These all classes are defined in java.lang package, hence we don’t need to import them manually.

Java wrapper classes

Why wrapper classes?

1. Java Collection Framework works only with objects.

For example:

Although, we can also pass primitive values. Then how does it work?

If a primitive value is passed then java converts the primitive value to its corresponding wrapper classes before storing it. This is called autoboxing.

How to convert primitive to object in java

Now the next question that comes to your mind is: how do we convert a primitive value to a corresponding wrapper class. For example, int to Integer or a double to Double?

We can convert the primitive value to its corresponding object by using a constructor or the static factory method.

For example –

We should always use the static factory method over the constructor to convert the primitive values to objects. As the compiler does some performance optimization which is not possible if we use a constructor.

Java Integer Cache

How to convert wrapper to primitive in java

If we want to convert objects into their primitive types, we can use the corresponding method such as doubleValue(), floatValue(), intValue() etc. The method is present in each wrapper class.

Autoboxing and Unboxing in Java

Let’s discuss the concept of autoboxing and unboxing that is related to wrapper classes.

Autoboxing is the automatic conversion of a primitive value into an object of the corresponding wrapper class.

For example –

When we add primitive value to a collection.

The other example is to assign the primitive value to an object type.

In all these cases, internally java uses the valueOf() method to convert primitive to their corresponding wrapper class.

The java compiler converts the previous code to the following at runtime:

Unboxing on other hand is the automatic conversion of an object of a wrapper type into its corresponding primitive type.

Autoboxing and unboxing are done by the java compiler, we don’t have to do anything in it.

Recommended Java Books

Java the complete reference

Effective java

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.