Wrapper Classes in Java: What is it, Why do we need it?
Updated on Nov 14, 2022 | 5 min read | 17.0k views
Share:
For working professionals
For fresh graduates
More
Updated on Nov 14, 2022 | 5 min read | 17.0k views
Share:
Table of Contents
Java works on the concepts of OOP – Object-Oriented Programming. Despite that, Java can not be considered as a complete object-oriented language. That is because Java works with eight primitive data types – byte, short, int, float, long, and double. These Java data types are not objects, which makes Java not wholly object-oriented.
However, wrapper classes provide a good way around that limitation by converting primitive data types into objects. In that way, wrapper classes make the Java code completely object-oriented.
This is one of the more important concepts to understand for everybody beginning with the Java programming language. Let’s help you with that through this article. First, we’ll look at wrapper classes in Java and why we need them and their advantages. By the end of the article, you’ll be in a position to work with wrapper classes without being confused!
Check out our free courses to get an edge over the competition.
Learn Online software development programs from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
Software developers often stumble upon situations that would require them to not work with primitive data types but with objects. To achieve this conversion of primitive data type to objects, Java provides programmers with the concept of the wrapper class. This class helps in conversion from primitive to object as well as from object to primitive data type.
Creating a wrapper class automatically creates a new field where the primitive data types are stored. The object of the wrapper class then holds the primitive data type. This process of converting primitive data types into objects using wrapper classes is known as boxing. While working with wrapper classes, you need to pass the primitive data type’s value to the constructor of the wrapper class.
Check out upGrad’s Advanced Certification in DevOps
The six wrapper classes – byte, short, int, float, long, and double – are all subclasses of the number class, which is an abstract class. The remaining two wrapper classes – boolean and character – are subclasses of the object class.
Here’s a quick summary of the wrapper class for different primitive data types, along with the constructor argument required to create the wrapper class:
Wrapper classes in Java are beneficial for situations where the program demands working with an object instead of a primitive data type. In that sense, wrapper classes help the Java program be 100% object-oriented. Apart from that, wrapper classes in Java are needed for the following reasons:
Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)
Wrapper class in Java provides the following primary benefits:
public class Example{
public static void main(String args[]){
//Converting float primitive into Float object
float n=10.10;
Float obj=Float.valueOf(n);
System.out.println(n+ ” “+ obj);
}
}
Output:
10.10 10.10
As the output shows, both the primitive data as well as the object hold the same value. So, you can access the same value using obj or n, whatever you prefer, according to the situation.
public class Example{
public static void main(String args[]){
//Creating Wrapper object
Float o= new Float(50.00);
//Converting the object to primitive
float n= obj.floatValue();
System.out.println(n+ ” “+ o);
}
}
Output:
50.00 50.00
The float object has been converted back to the float primitive data type in the above example. Likewise, you can do a lot of conversion and manipulations using wrapper classes in Java. There are different functions that wrapper classes work with. That discussion is beyond the scope of this article, but we recommend you explore wrapper classes in-depth, along with the different functions you will need to work with when handling wrapper classes.
Like wrapper classes, there are also many other essential concepts and ideas that you need to master to excel in software development. At upGrad, we have mentored various students around the globe and helped them start their careers in their desired field. Our personalised training sessions and expert-led case study discussions help students stay on top of all the concepts acquired.
To help students kickstart their software development career, we present to you a 5-month online program created by experts and industry leaders – Job-Linked PG Certification in Software Engineering. Apart from teaching you all the nuances of the field, the program will also help you get placed with top companies. So check out this course and get yourself registered today!
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources