java
- IntroductionInstallation and SetupJava Syntax and StructureRunning Your First ProgramJava VariablesData Types in JavaOperators in JavaJava Input and OutputControl StatementsLoops in JavaMethods in Java (Functions)Arrays in JavaString Handling in JavaOOPS Concept in JavaClasses and ObjectsConstructor and Constructor Overloadingthis KeywordStatic MembersInheritanceAggregation in JavaMethod OverloadingMethod Overridingsuper KeywordFinal Keyword with Class, Method, and VariablesAccess Modifiers in javaEncapsulation in Java Polymorphism in JavaAbstraction in JavaAbstract ClassesInterfaces in JavaDifference between Abstract Class and Interface Nested and Inner Classes Exception HandlingJava PackagesWrapper Classes and AutoboxingJava Collections FrameworkFile Handling in JavaMultithreadingBasics of Java Memory Management and Garbage CollectionJava JDBC
Wrapper Classes and Autoboxing in Java – Complete Tutorial
Last Updated on: 25th Nov 2025 13:08:49 PM
Java is an Object-Oriented Programming (OOP) language, but primitive data types like int, char, float, etc. are not objects.
To bring primitives into the object world, Java provides Wrapper Classes.
Wrapper classes allow primitives to behave like objects—this is extremely useful in:
-
Collections (ArrayList, HashMap, etc.)
-
Frameworks (Hibernate, Spring)
-
Method parameter passing
-
Converting strings to numbers
-
Handling null values
What Are Wrapper Classes?
The wrapper class in Java provides the mechanism to convert primitive into object and object into primitive.
Java provides 8 wrapper classes, one for each primitive data type:
| Primitive Type | Wrapper Class |
|---|---|
byte |
Byte |
short |
Short |
int |
Integer |
long |
Long |
float |
Float |
double |
Double |
char |
Character |
boolean |
Boolean |
Why do we need Wrapper Classes?
Because primitives:
-
cannot be stored in collections
-
cannot be null
-
don’t have methods
-
are not objects
Wrapper classes provide useful methods like:
-
Integer.parseInt() -
Double.valueOf() -
Character.isDigit()
Example:
int x = 10;
Integer obj = Integer.valueOf(x); // object form
System.out.println(obj.toString());
Real-Life Example: Why Wrapper Classes Are Needed?
Imagine you have an Online Shopping Cart implemented using ArrayList.
ArrayList stores only objects.
So you cannot write:
ArrayList<int> cart = new ArrayList<>(); // ❌ Not allowed
You must use:
ArrayList<Integer> cart = new ArrayList<>(); // ✔ Allowed
Here, Integer is the wrapper for int.
Autoboxing & Unboxing – The Most Important Topic
Java 5 introduced Autoboxing and Unboxing to reduce manual conversion. autoboxing and unboxing feature convert primitives into objects and objects into primitives automatically.
1. Autoboxing (Primitive → Wrapper Automatically)
Java automatically converts a primitive value to its wrapper object.
int a = 50;
Integer obj = a; // autoboxing
System.out.println(obj);
Output:
50
2. Unboxing (Wrapper → Primitive Automatically)
Java automatically converts an object of wrapper class back to a primitive.
Example:
Integer num = 100;
int n = num; // unboxing
System.out.println(n);
Output:
100
Autoboxing & Unboxing – Full Practical Example
public class AutoBoxingExample {
public static void main(String[] args) {
// Autoboxing
int a = 20;
Integer obj = a;
System.out.println("Autoboxing: " + obj);
// Unboxing
Integer num = 40;
int b = num;
System.out.println("Unboxing: " + b);
}
}
Output:
Autoboxing: 20
Unboxing: 40
Manual Conversion (Before Autoboxing)
Before Java 5:
int a = 10;
Integer obj = Integer.valueOf(a); // manual boxing
Integer num = 20;
int b = num.intValue(); // manual unboxing
Wrapper Class Useful Methods (Very Important for Interviews)
parseInt()
Converts String → int
int x = Integer.parseInt("123");
valueOf()
Converts primitive → wrapper object
Integer n = Integer.valueOf(10);
toString()
Converts number → String
String s = Integer.toString(100);
Boolean Methods
Boolean bool = Boolean.valueOf(true);
Real-Life Example: Taking User Input
Scanner inputs come as Strings.
To convert:
Scanner sc = new Scanner(System.in);
System.out.print("Enter price: ");
String p = sc.nextLine();
int price = Integer.parseInt(p); // using Wrapper
Real-Life Example: Banking Transaction System
Bank logs are stored in objects, not primitives.
ArrayList<Double> transactions = new ArrayList<>();
transactions.add(1500.75); // autoboxing
transactions.add(200.50);
No need to manually convert.
Important: Wrapper Classes Can Store NULL
Primitive:
int a = null; // ❌ Error
Wrapper:
Integer a = null; // ✔ Allowed
Used in:
-
Frameworks
-
Databases (nullable fields)
-
API responses
Wrapper Classes vs Primitive Types
| Feature | Primitive | Wrapper |
|---|---|---|
| Memory | Fast & small | More memory |
| Methods | No | Yes |
| Can be null? | No | Yes |
| Used in Collections | No | Yes |
| Speed | Faster | Slower |
When to Use Wrapper Classes?
Use Wrapper Classes when:
✔ Working with Collections
✔ Handling null values
✔ Using generics
✔ Converting string inputs
✔ Working with APIs / JSON / Databases
✔ Using Reflection
Complete Summary
-
Wrapper classes convert primitives into objects.
-
Autoboxing converts primitive → wrapper automatically.
-
Unboxing converts wrapper → primitive automatically.
-
Required for Collections, Frameworks, Generics, and real-world applications.
-
Provide useful methods for data conversion.
Keep practicing — you're doing amazing!
Happy Coding! ![]()
.png)