Wrapper Classes
Primitives as Objects! Understand Autoboxing and Unboxing in Java.
Primitives as Objects! Understand Autoboxing and Unboxing in Java. This hands-on tutorial focuses on practical implementation of wrapper classes concepts.
Wrapper Classes
Wrapper classes provide a way to use primitive data types (int, boolean, etc.) as objects.
Sometimes you must use wrapper classes, for example when working with Collection objects like ArrayList, where primitive types cannot be used.
Primitive vs Wrapper
| Primitive | Wrapper Class |
|---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
Creating Wrapper Objects
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
Autoboxing & Unboxing
-
Autoboxing: Automatic conversion of primitive type to its corresponding object wrapper class.
Character ch = 'a'; // Autoboxing -
Unboxing: Automatic conversion of wrapper class to primitive type.
char c = ch; // Unboxing
Interactive Code
See how wrappers work.
AI Mentor
Confused about "Java Wrapper Classes, Autoboxing and Unboxing"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3Which is the wrapper class for `int`?
Next Steps
Programs don't always run smoothly. What happens when things go wrong? Module 7: Exception Handling.