Wrapper Classes in Java

Video Tutorial
FREE
Wrapper Classes thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

The wrapper class in Java provides a way to represent the value of primitive data types as an object. By creating an object of the wrapper class, a field is created, and primitive data types can be stored in this field.

Why Do We Need Wrapper Classes?

There are different usecases where we need the wrapper classes in java, some of them mentioned below:

  • Whenever primitive types are required as objects, wrapper classes can be used. Wrapper classes also include methods to unwrap the object and give back the data type.
  • In java.util package, the classes handle only objects. In this case, wrapper classes in Java are helpful as they convert the primitive data types to objects.
  • In the Collection framework, Data Structures such as ArrayList store data only as objects, not primitive types.
  • Wrapper classes have methods that support object creation from other object types, such as string.
  • Wrapper classes are also used for synchronization in multithreading. As in the synchronization process, we try to ensure that the shared resource will be used by only one thread at a time. Objects are needed for this.

Advantages of Wrapper Classes in Java

  • In Java, there are instances where utilizing objects instead of primitive data types is necessary, such as when interacting with collections. As we can see in the below example, we need to use Integers, not the primitive int. The primitive only specifies the range and type of the value, like here, it is int. The wrapper class in Java wraps that value into an object of the specified type, which is the Integer type.
  • We can use the wrapper class objects in Java and store them as a null value. In real-time scenario applications, the values can be null; hence, we need to store null values.

  • A wrapper type allows a primitive to operate in more advanced ways. An integer can be used in different ways, like a class described as Hours, for example, which presents the number meaning wherever it is used.

  • The primitive types just operate with value; the wrapper class in Java provides it with a name. For example, int as Integer, char as Character, etc. It means that int only specifies the range and type of the value, but by creating the object with wrapper class Integer, it will be given a reference name(object).

Primitive Data Types and Their Corresponding Wrapper Classes

Primitive Data Types are the basic building blocks of data manipulation in Java. They are in-built or predefined in Java. The eight Primitive Data Types include byte, short, int, long, float, double, boolean and char. The language predefines this.

Data Types in Java

Generic classes work with objects and don't support Primitives. As a result, Wrapper classes are needed as they convert primitive data types into objects. The Java programming language provides the java.lang package has classes that are fundamental to the design, and the most important classes among them are Object and Class. So, Java wrapper classes wrap or represent the values of primitive data types as an object. When an object is created using a wrapper class, it contains a field that can store the primitive data types.

The object of one type only contains a field of that particular type, which means a double type of object contains a double type of field only. Below are the Primitive Data Types and their corresponding Wrapper classes:

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

Autoboxing

Autoboxing is when the Java compiler automatically converts the primitive data types to the object of their corresponding wrapper classes. For example, converting an int to Integer, a double to Double, etc.

The condition when the Java compiler applies autoboxing when:

  • The variable is assigned to the corresponding wrapper class.
  • It was passed as a parameter to a method requiring an object of the corresponding wrapper class.

For example:

Output:

Here, the output is 50 for all as- The 'a' variable is assigned with int value 50. The 'first' variable is assigned with the value of 'a' that is 50 only that's how int converts into Integer explicitly.

The 'second' variable will also have the value of 'a', which is 50, as due to autoboxing, the compiler internally does the conversion automatically.

Unboxing

It is just the opposite process of autoboxing. Unboxing automatically converts an object of a wrapper type (Integer, for example) to its corresponding primitive (int) value.

The Java compiler applies unboxing when:

  • An Object of a wrapper class in Java is passed as a parameter to a method expecting a value of the corresponding primitive type
  • The object of the wrapper class is assigned to a variable of the corresponding primitive type.

For example:

Output:

Here, the output for all is 5 as the object 'a' is created with Integer passing the value 5. So the value of 'a' is 5.the 'first' variable is assigned with a.intValue(). The value of 'a' is 5, so the value of 'first' will be 5.
The 'second' variable is assigned directly with 'a'. Due to unboxing. The compiler internally assigns the intValue of 'a' that is 5.

Java Wrapper Classes Example

You might have gained an understanding of what a wrapper class is in Java. Now, let's discuss examples that cover the usage of wrapper classes in Java.

Output:

Explanation:

In the above example, we have used the valueOf() method to convert the primitive types into objects. We have used the instanceof operator to check whether the generated objects are of Integer or Double type or not. However, the Java compiler can directly convert the primitive types into corresponding objects. Then, we have used the intValue() and doubleValue()methods to convert the Integer and Double objects into corresponding primitive types. However, the Java compiler can automatically convert objects into corresponding primitive types.

Custom Wrapper Classes in Java

As the Java Wrapper classes wrap the primitive data types. Likewise, we can also create custom wrapper class in Java which wraps a primitive data type.

Output:

Here, we have created a custom wrapper class that is SpeedWrapperClass() and to set the value of speed we have also created constructors. Now, when we created an instance of the custom wrapper class and passed the integer value it worked as an Integer wrapper class which wraps the int value of 100 and sets the speed as 100.

Conclusion

  • Wrapper classes in Java wrap the primitive data type in its class object.
  • Java wrapper classes are provided in the java.lang package.
  • Autoboxing and unboxing converts the primitive into objects and objects into primitives automatically.
  • We can also create a custom Wrapper class in Java, which wraps a primitive data type.

FAQs

Q: What is an example of a wrapper?

A: An example of a wrapper in Java is the Integer class, which wraps the primitive data type int to provide additional functionality.

Q: Which package is wrapper class?

A: The wrapper class in Java package is java.lang.

Q: How many wrapper classes are there in Java?

A: There are eight wrapper classes in Java.