Wrapper classes in Java

Last Updated : 7 Jan 2026

Java wrapper class provides a mechanism to convert primitive data types into objects and objects into primitive data types. In this chapter, we will learn about wrapper classes and how they work.

What are Wrapper classes in Java?

Wrapper classes are predefined classes in the java.lang package that convert primitive data types into objects. They allow primitives to be used in object-oriented features such as collections and methods that require objects.

Autoboxing and Unboxing

Since J2SE 5.0, Java supports autoboxing and unboxing, which automatically convert primitive data types into their corresponding object types and vice versa.

The automatic conversion of a primitive type into its wrapper object is called autoboxing, while the conversion of a wrapper object back into its primitive type is known as unboxing.

Use of Wrapper Classes in Java

Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.

  • Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
  • Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.
  • Synchronization: Java synchronization works with objects in Multithreading.
  • java.util package: The java.util package provides the utility classes to deal with objects.
  • Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.

Wrapper Classes in the java.lang Package

The eight classes of the java.lang package are known as wrapper classes in Java. The list of eight wrapper classes is given below:

Primitive TypeWrapper class
booleanBoolean
charCharacter
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

Primitive to Wrapper Conversion (Autoboxing)

The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float, boolean to Boolean, double to Double, and short to Short.

Since Java 5, we do not need to use the valueOf() method of wrapper classes to convert the primitive into objects.

Example

The following example demonstrates the primitive to wrapper class conversion using autoboxing.

Output:

20 20 20

Wrapper to Primitive Conversion (Unboxing)

The automatic conversion of wrapper type into its corresponding primitive type is known as unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue() method of wrapper classes to convert the wrapper type into primitives.

Example

The following example demonstrates the wrapper to primitive type conversion using unboxing:

Output:

3 3 3

Wrapper Classes (Autoboxing and Unboxing) Example

In this example, we are converting all primitive data types into their corresponding wrapper class objects using autoboxing. After that, the wrapper objects are converted back into primitive data types using unboxing. Finally, both object values and primitive values are printed to verify the conversion process.

Output:

---Printing object values---
Byte object: 10
Short object: 20
Integer object: 30
Long object: 40
Float object: 50.0
Double object: 60.0
Character object: a
Boolean object: true
---Printing primitive values---
byte value: 10
short value: 20
int value: 30
long value: 40
float value: 50.0
double value: 60.0
char value: a
boolean value: true

Custom Wrapper Class

Wrapper classes wrap primitive data types; that is why they are called wrapper classes. Similarly, we can also create our own class that wraps a primitive data type. Such a user-defined class is known as a custom wrapper class.

Example

The following example demonstrates creating and using a custom wrapper class.

Output:

10