This article will provide a comprehensive overview of java wrapper classes and the below-listed concepts will be covered in this write-up:
- What are Wrapper Classes in java?
- Why Wrapper Classes?
- How do Wrapper Classes work?
- How to Use Java Number Methods.
So let’s get started!
Wrapper Classes in Java
The classes that assist us in creating and utilizing the object of the primitive data types are known as the java wrapper classes. For example, Character is a wrapper class for char, Float for float, Integer for int, and so on.
Let’s consider the table provided below for a profound understanding of the primitive types and their respective wrapper classes:
Primitive Data Types | Wrapper Classes |
---|---|
int | Integer |
float | Float |
short | Short |
long | Long |
double | Double |
char | Character |
byte | Byte |
boolean | Boolean |
All these wrapper classes are inherited from an abstract class Number.
Why Wrapper Classes
Sometimes we need to work with the class objects but using primitive data types we can’t create the object, therefore to deal with such situations, we have to utilize the wrapper classes.
Let’s assume we are working with LinkedList, or ArrayList then we can avail the functionalities of such classes only by using their objects and we know that objects can’t be created with primitive types. Therefore, we have to utilize the java Wrapper classes in such scenarios.
Let’s consider the below-given screenshot for a profound understanding of this concept:
The above snippet verifies that we succeed in creating the object of <LinkedList> using the wrapper class however, an error occurs while the object creation of <LinkedList> using the primitive data type “int”.
How to Work with Wrapper Class
In order to work with java wrapper classes, we have to create the object of the respective wrapper class.
Example
In the below given snippet we create the objects of four different wrapper classes and assign them some values:
Float floatObject = 52.93f;
Integer intObject = 405;
Double doubleObject = 99.99;
Character charObject = 'A';
System.out.println(intObject);
System.out.println(floatObject);
System.out.println(doubleObject);
System.out.println(charObject);
}
We utilize the objects of each wrapper class to print the values:
The above snippet validates the working of each wrapper class.
How to use Number Methods in Java
One of the significant features of wrapper classes is that we can utilize the Number Methods by using the objects of wrapper classes. Using these methods we can achieve different functionalities such as typecasting one data type to another, converting objects to strings, etc.
In java a wide range of number methods such as intValue(), booleanValue(), shortValue(), longValue(), byteValue(), floatValue(), charValue(), and doubleValue(), can be utilized to convert one data type to other. Moreover, many more methods are available in java to perform various functionalities such as parseInt(), toString(), equals(), floor() etc.
Example
The doubleValue() and intValue() methods are used in the below code snippet to convert the integer to double, and double to integer values respectively.
Integer intObject = 405;
Double doubleObject = 99.99;
System.out.println(intObject.doubleValue());
System.out.println(doubleObject.intValue());
}
The complete code and its respective output is shown in the below-given snippet:
In the above snippet, the “.0” in the first value shows that it is converted into the double value and skipping the “.99” from the second value represents that it is successfully converted into an integer value.
Conclusion
The wrapper classes contain the primitive data types and provide a way to use them as an object. The wrapper classes assist us in converting the primitive data types to objects and using these objects we can achieve different functionalities by means of java number methods. Moreover, by using numbers methods we can convert one data type to another and we can attain many more enhancements by means of these methods. This write-up provides a comprehensive overview of what wrapper classes are, the need for wrapper classes, and how to work with wrapper classes in java.