In the OOPs concepts guide, we learned that object oriented programming is all about objects. The eight primitive data types byte, short, int, long, float, double, char and boolean are not objects, Wrapper classes are used for converting primitive data types into objects, like int to Integer, double to Double, float to Float and so on. Let’s take a simple example to understand why we need wrapper class in java.
For example: While working with collections in Java, we use generics for type safety like this: ArrayList<Integer> instead of this ArrayList<int>. The Integer is a wrapper class of int primitive type. We use wrapper class in this case because generics needs objects not primitives. There are several other reasons you would prefer a wrapper class instead of primitive type, we will discuss them as well in this article.
Primitive Data Type | Corresponding Wrapper class |
boolean | Boolean |
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
Why we need wrapper class in Java
1. As I mentioned above, one of the reason why we need wrapper is to use them in collections API. On the other hand, the wrapper objects hold much more memory compared to primitive types. So use primitive types when you need efficiency and use wrapper class when you need objects instead of primitive types.
The primitive data types are not objects so they do not belong to any class. While storing in data structures which support only objects, it is required to convert the primitive type to object first which we can do by using wrapper classes.
Example:
HashMap<Integer, String> hm = new HashMap<Integer, String>();
So for type safety we use wrapper classes. This way we are ensuring that this HashMap keys would be of integer type and values would be of string type.
2. Wrapper class objects allow null values while primitive data type doesn’t allow it.
Lets take few examples to understand how the conversion works:
Wrapper Class Example 1: Converting a primitive type to Wrapper object
public class JavaExample{ public static void main(String args[]){ //Converting int primitive into Integer object int num=100; Integer obj=Integer.valueOf(num); System.out.println(num+ " "+ obj); } }
Output:
100 100
As you can see both primitive data type and object have same values. You can use obj in place of num wherever you need to pass the value of num as an object. The conversion of primitive data type to object is known as autoboxing and the conversion from object to primitive type is known as unboxing, this concept is covered in detail at: Autoboxing and Unboxing in Java.
Wrapper Class Example 2: Converting Wrapper class object to Primitive
public class JavaExample{ public static void main(String args[]){ //Creating Wrapper class object Integer obj = new Integer(100); //Converting the wrapper object to primitive int num = obj.intValue(); System.out.println(num+ " "+ obj); } }
Output:
100 100
Custom Wrapper Class
We can also create a custom wrapper class to wrap a primitive type to an object. Here, we have a int data type that belongs to class XYZ
. We can use this primitive data type as object using the constructor and getter setter methods of XYZ
class as shown below:
class XYZ{ private int num; //default constructor XYZ(){} //parameterized constructor XYZ(int num){ this.num=num; } //getter and setter methods public int getIntValue(){ return num; } public void setIntValue(int i){ this.num=i; } @Override public String toString() { return Integer.toString(num); } } public class JavaExample{ public static void main(String[] args){ XYZ obj = new XYZ(10); System.out.println(obj); obj.setIntValue(100); System.out.println(obj.getIntValue()); } }
Output:
10 100
Conclusion
In this guide, we learned what are the advantages of objects over primitive data types. How to convert primitive types to objects using wrapper class. We also learned when to use primitive types and when to use objects. If you want to learn more such topics related to Java then head over to the Java Tutorial section.
Leave a Reply