In this post, we’ll be learning everything about wrapper classes. We’ll understand what wrapper class is, why it is introduced and what are autoboxing and unboxing. So, without further ado, let’s dive into it.
Wrapper Classes
The wrapper classes in Java are used to convert primitive types ( int, char, float, boolean, etc.) into corresponding objects.
Wherever the data type is required as an object, this object can be used.
There are eight wrapper classes available in java.lang package. Each of the 8 primitive types has corresponding wrapper classes. They are:
- Byte
- Short
- Integer
- Long
- Float
- Double
- Character
- Boolean
Wrapper classes also include methods to unwrap the object and give back the data type, known as unboxing. We’ll be discussing in more detail about this in the later part of this post.
Why Wrapper Classes Were Introduced in Java?
Since the primitive data types are not objects, they cannot be directly stored in collections. Generic classes work with objects and don’t support Primitives. The Wrapper classes like Integer, Long, Boolean provide a way to store primitive values as objects. It’s needed
– to represent primitive datatype into its object type.
– to use it in collections such as ArrayList and HashMap where only objects are stored.
– to store null values
– to convert Strings into data types (known as parsing operations), here methods of type parseXXX() are used i.e. Integer.parseInt() and Float.parseFloat()
Note: We can also use the valueOf() method to convert primitive types into corresponding objects.
If you have been using Collections like Stack or Vector before Java 1.5, then you are familiar with the issues like you can not directly put primitives into Collections, instead, you first need to convert them into Object, and then only you can put them into Collections.
Example –
1.
// generates an error
ArrayList
// runs perfectly
ArrayList
2.
// generates an error
int a = null;
// runs perfectly
Integer a = null;
AutoBoxing:
Autoboxing in Java refers to the automatic conversion of primitive data types to their corresponding wrapper classes.
Eg:
int x = 10;
float y = 5.64f;
//Autoboxing
Integer intObj = x;
Float floatObj = y;
This was introduced in Java 5. Prior to Java 5, the conversion of a primitive value to its corresponding wrapper class in Java was done manually, using the constructor of the wrapper class.
Unboxing:
Unboxing in Java refers to the automatic conversion of a wrapper object to its primitive data type.
Eg:
Integer x = 10;
Float y = 5.64F;
Integer z = new Integer(43);
//Unboxing
int a = x;
int b = y;
int c = z.intValue();
Conclusion
Now that we’ve learned everything about wrapper classes we should be able to answer What wrapper classes are, why it is introduced, and what is autoboxing and unboxing. Further, we expect you to effectively utilize your knowledge and implement them in your project accordingly. Furthermore, If there is any update in the future, we will be updating this accordingly. Feel free to drop your views if you really enjoyed this post. Happy Coding!
The greatest glory in living lies not in never falling, but in rising every time we fall.
Nelson Mandela