In Java, we have 8 primitive data types, such as byte, short, long, which we use to declare variables of different types to hold corresponding values based on the business logic.
However, there are instances when we need to represent primitive type values as objects. This is where the concept of Wrapper classes comes into play.
Wrapper classes are essentially used to encapsulate primitive types as objects.
Use of Wrapper Class:
1. Wrapper classes provide various utility methods.
2. In the Collection Framework, which includes data structures like ArrayList, Vector, HashSet, etc., we work with objects. Wrapper classes allow us to work with primitive types in the Collection Framework.
3. Wrapper classes enable the application of cloning, serialization, and synchronization implementations.
Primitive Data Type | Wrapper Class |
---|---|
byte | java.lang.Byte |
short | java.lang.Short |
int | java.lang.Integer |
long | java.lang.Long |
float | java.lang.Float |
double | java.lang.Double |
boolean | java.lang.Boolean |
char | java.lang.Character |