Last Updated : 06 Aug, 2025
A Wrapper class in Java is one whose object wraps or contains primitive data types. When we create an object in 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. Let's check on the wrapper classes in Java with examples.
Example: Demonstration to convert the primitive to its corresponding wrapper.
Java
class Geeks
{
public static void main(String[] args)
{
// Primitive data type
int b;
// Integer wrapper class
Integer a;
// assigning value to primitive
b = 357;
// auto-boxing or auto wrapping converting primitive int to Integer object
a = b;
System.out.println("The primitive int b is: " + b);
System.out.println("The Integer object a is: " + a);
}
}
The primitive int b is: 357 The Integer object a is: 357
Explanation: In the above example, we first create an primitive int b and assign a value 357. Then we assign this primitive value to an Integer object b and it automatically covert it to primitve to a Integer object(Autoboxing).
Need of Wrapper ClassesThere are certain needs for using the Wrapper class in Java as mentioned below:
Below are given examples of wrapper classes in Java with their corresponding Primitive data types in Java.
Autoboxing and Unboxing 1. AutoboxingThe 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 the automatic conversion of primitive to wrapper class (Autoboxing).
Java
import java.util.ArrayList;
class Geeks
{
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));
}
}
2. 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.
Example: Java program demonstrates automatic conversion of wrapper class to primitive (Unboxing).
Java
import java.util.ArrayList;
class Geeks
{
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);
}
}
Java Wrapper Classes Example Java
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// byte data type
byte a = 1;
// wrapping around Byte object (use valueOf or autoboxing)
Byte byteobj = Byte.valueOf(a);
// int data type
int b = 10;
// wrapping around Integer object (use valueOf or autoboxing)
Integer intobj = Integer.valueOf(b);
// float data type
float c = 18.6f;
// wrapping around Float object (use valueOf or autoboxing)
Float floatobj = Float.valueOf(c);
// double data type
double d = 250.5;
// Wrapping around Double object (use valueOf or autoboxing)
Double doubleobj = Double.valueOf(d);
// char data type
char e = 'a';
// wrapping around Character object (autoboxing)
Character charobj = e;
// printing the values from objects
System.out.println(
"Values of Wrapper objects (printing as objects)");
System.out.println("\nByte object byteobj: "
+ byteobj);
System.out.println("\nInteger object intobj: "
+ intobj);
System.out.println("\nFloat object floatobj: "
+ floatobj);
System.out.println("\nDouble object doubleobj: "
+ doubleobj);
System.out.println("\nCharacter object charobj: "
+ charobj);
// objects to data types unwrapping objects to primitive datatypes
byte bv = byteobj;
int iv = intobj;
float fv = floatobj;
double dv = doubleobj;
char cv = charobj;
// printing the values from data types
System.out.println(
"\nUnwrapped values (printing as data types)");
System.out.println("\nbyte value, bv: " + bv);
System.out.println("\nint value, iv: " + iv);
System.out.println("\nfloat value, fv: " + fv);
System.out.println("\ndouble value, dv: " + dv);
System.out.println("\nchar value, cv: " + cv);
}
}
Output:
Output Custom Wrapper Classes in JavaJava Wrapper classes wrap the primitive data types. We can create a class that wraps data inside it. So let us check how to create our own custom wrapper class in Java. It can be implemented for creating certain structures like queues, stacks, etc.
Example:
Java
import java.io.*;
// wrapper class
class Maximum {
private int maxi = 0;
private int size = 0;
public void insert(int x)
{
this.size++;
if (x <= this.maxi)
return;
this.maxi = x;
}
public int top() { return this.maxi; }
public int elementNumber() { return this.size; }
};
class Geeks {
public static void main(String[] args)
{
Maximum x = new Maximum();
x.insert(12);
x.insert(3);
x.insert(23);
System.out.println("Maximum element: " + x.top());
System.out.println("Number of elements inserted: "
+ x.elementNumber());
}
}
Maximum element: 23 Number of elements inserted: 3Advantages of Wrapper Classes
Primitive Data Type
Wrapper Class
char
Character
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
boolean
Boolean
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4