A Wrapper class is a class whose object wraps or contains primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object.
Need of Wrapper Classes
- They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).
- The classes in java.util package handles only objects and hence wrapper classes help in this case also.
- Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
- An object is needed to support synchronization in multithreading.
Primitive Data types and their Corresponding Wrapper class

Autoboxing and Unboxing
Autoboxing: Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example – conversion of int to Integer, long to Long, double to Double etc.
Example:
// Java program to demonstrate Autoboxing import java.util.ArrayList; class Autoboxing { public static void main(String[] args) { char ch = 'a'; // Autoboxing- primitive to Character object conversion Character a = ch; ArrayList<Integer> arrayList = new ArrayList<Integer>(); // Autoboxing because ArrayList stores only objects arrayList.add(25); // printing the values from object System.out.println(arrayList.get(0)); } } |
Output:
25
Unboxing: It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example – conversion of Integer to int, Long to long, Double to double, etc.
// Java program to demonstrate Unboxing import java.util.ArrayList; class Unboxing { public static void main(String[] args) { Character ch = 'a'; // unboxing - Character object to primitive conversion char a = ch; ArrayList<Integer> arrayList = new ArrayList<Integer>(); arrayList.add(24); // unboxing because get method returns an Integer object int num = arrayList.get(0); // printing the values from primitive data types System.out.println(num); } } |
Output:
24
Implementation
// Java program to demonstrate Wrapping and UnWrapping // in Java Classes class WrappingUnwrapping { public static void main(String args[]) { // byte data type byte a = 1; // wrapping around Byte object Byte byteobj = new Byte(a); // int data type int b = 10; //wrapping around Integer object Integer intobj = new Integer(b); // float data type float c = 18.6f; // wrapping around Float object Float floatobj = new Float(c); // double data type double d = 250.5; // Wrapping around Double object Double doubleobj = new Double(d); // char data type char e='a'; // wrapping around Character object Character charobj=e; // printing the values from objects System.out.println("Values of Wrapper objects (printing as objects)"); System.out.println("Byte object byteobj: " + byteobj); System.out.println("Integer object intobj: " + intobj); System.out.println("Float object floatobj: " + floatobj); System.out.println("Double object doubleobj: " + doubleobj); System.out.println("Character object charobj: " + charobj); // objects to data types (retrieving data types from objects) // unwrapping objects to primitive data types byte bv = byteobj; int iv = intobj; float fv = floatobj; double dv = doubleobj; char cv = charobj; // printing the values from data types System.out.println("Unwrapped values (printing as data types)"); System.out.println("byte value, bv: " + bv); System.out.println("int value, iv: " + iv); System.out.println("float value, fv: " + fv); System.out.println("double value, dv: " + dv); System.out.println("char value, cv: " + cv); } } |
Output:
Values of Wrapper objects (printing as objects) Byte object byteobj: 1 Integer object intobj: 10 Float object floatobj: 18.6 Double object doubleobj: 250.5 Character object charobj: a Unwrapped values (printing as data types) byte value, bv: 1 int value, iv: 10 float value, fv: 18.6 double value, dv: 250.5 char value, cv: a
Related Article :
Comparison of Autoboxed Integer objects in Java
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Primitive Wrapper Classes are Immutable in Java
- Utility methods of Wrapper classes | valueOf(), xxxValue(), parseXxx(), toString()
- Compute modulus division by a power-of-2-number using Wrapper Class
- Access specifiers for classes or interfaces in Java
- Abstract Classes in Java
- Private Constructors and Singleton Classes in Java
- Parent and Child classes having same data member in Java
- Nested Classes in Java
- Understanding Classes and Objects in Java
- Why Java Interfaces Cannot Have Constructor But Abstract Classes Can Have?
- Classes and Objects in Java
- Creating an Server-Client Application using the DatagramPacket and DatagramSocket classes
- Messages, aggregation and abstract classes in OOPS
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
Improved By : PasinduSiriwardana

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
