In Java, each primitive data type has a corresponding wrapper class. This tutorial explains what wrapper classes are and when to use them.
What are Wrapper Classes?
Wrapper classes provide a way to use primitive data types as objects. Java's collection framework (e.g., ArrayList) works only with objects, so wrapper classes are needed to store primitive values in collections.
| Primitive | Wrapper Class |
|---|---|
| int | Integer |
| double | Double |
| char | Character |
| boolean | Boolean |
| float | Float |
| long | Long |
| byte | Byte |
| short | Short |
Autoboxing and Unboxing
Autoboxing is the automatic conversion of a primitive to its wrapper class. Unboxing is the reverse. Since Java 5, these conversions happen automatically:
Integer num = 5; // autoboxing int val = num; // unboxing
For more on data types, see Java Data Types. For OOP concepts, see OOP in Java.