Wrapper classes in JavaLast 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 UnboxingSince 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 JavaJava 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.
Wrapper Classes in the java.lang PackageThe eight classes of the java.lang package are known as wrapper classes in Java. The list of eight wrapper classes is given below:
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. ExampleThe 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. ExampleThe following example demonstrates the wrapper to primitive type conversion using unboxing: Output: 3 3 3 Wrapper Classes (Autoboxing and Unboxing) ExampleIn 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 ClassWrapper 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. ExampleThe following example demonstrates creating and using a custom wrapper class. Output: 10 |
We request you to subscribe our newsletter for upcoming updates.