Wrapper Classes in Java
Java is an Object Oriented Language which views everything as an object. For an instance a simple file can be treated as an object, an address of a system can be seen as an object, an image can be treated as an object (with java.awt.Image) and a simple data type can be converted into an object (with wrapper classes).
What are Wrapper Classes in Java?
Wrapper class are defined as a class in which a primitive value is wrapped up. These primitive wrapper classes are used to represent primitive data type values as objects. The Java platform provides wrapper classes for each of the primitive data types.
Why Wrapper Classes are introduced in Java?
- As Java is an Object Oriented Language, which views everything as an Object. To replace the primitive Data types in Java Wrapper classes are introduced.
- Second main thing is to support Collections API in Java. We know that collections take input in Object format, obviously primitive data types cannot be used with collection framework. Wrapper Classes replaces primitive data types in collections Framework in Java.
What are the uses of Wrapper Classes in Java?
- To convert simple data types into objects, that is, to give object form to a data type; here constructors are used.
- To convert strings into data types (known as parsing operations), here methods of type parseXXX() are used.
Hierarchy of Wrapper Classes
List of Wrapper Classes in Java
Primitive Data Type | Wrapper class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Enjoy Reading This Article?
Here are some more articles you might like to read next: