Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconWrapper Classes in Java: What is it, Why do we need it?

Wrapper Classes in Java: What is it, Why do we need it?

Last updated:
23rd Dec, 2021
Views
Read Time
5 Mins
share image icon
In this article
Chevron in toc
View All
Wrapper Classes in Java: What is it, Why do we need it?

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.

Ads of upGrad blog

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.

Wrapper class in Java

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:

Explore Our Software Development Free Courses

Why are wrapper classes in Java needed? 

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: 

  • To bind the values of different primitive data types into objects. This helps in performing complex operations and working with collections like HashMap, ArrayList, etc. 
  • To provide different utility functions that can be used with primitive data types. 
  • Since primitive data types can’t be given a null value, but wrapper classes can, they also act as a workaround for assigning a null value to any primitive data type. 

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN) 

What are the advantages of using wrapper classes in Java?

Wrapper class in Java provides the following primary benefits: 

  • They help the code be serialisable. This is because serialisation requires the conversion of objects into streams. If programmers want to serialise a primitive value, it must first be converted into objects using wrapper classes. 
  • They help with synchronisation because multithreading synchronisation in Java requires objects. 
  • Primitive data can benefit from various util classes of the Java.util package. 
  • Working with collection frameworks becomes possible using wrapper classes. 
  • Converting primitive data types into objects makes it possible to change the value inside the function using a call-by-value method. 
  • Wrapper classes in Java ensure that the program is polymorphic. 

Explore our Popular Software Engineering Courses

Examples

1. Converting a primitive type to object

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. 

In-Demand Software Development Skills

2. Converting object to primitive type

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. 

Ads of upGrad blog

Read our Popular Articles related to Software Development

In Conclusion

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!

Profile

upGrad

Blog Author
We are an online education platform providing industry-relevant programs for professionals, designed and delivered in collaboration with world-class faculty and businesses. Merging the latest technology, pedagogy and services, we deliver an immersive learning experience for the digital world – anytime, anywhere.

Frequently Asked Questions (FAQs)

1 Why are wrapper classes needed in Java?

Wrapper classes are fundamental in Java because they help a Java program be completely object-oriented. The primitive data types in java are not objects, by default. They need to be converted into objects using wrapper classes.

2How many wrapper classes are available in Java?

The number of wrapper classes in Java is the same as the number of primitive data types supported by Java, i.e. 8.

3Are wrapper classes in Java mutable?

Yes, wrapper classes are mutable.

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Best Jobs in IT without coding
134665
If you are someone who dreams of getting into the IT industry but doesn’t have a passion for learning programming, then it’s OKAY! Let me
Read More

by Sriram

12 Apr 2024

Scrum Master Salary in India: For Freshers & Experienced [2023]
900366
Wondering what is the range of Scrum Master salary in India? Have you ever watched a game of rugby? Whether your answer is a yes or a no, you might h
Read More

by Rohan Vats

05 Mar 2024

SDE Developer Salary in India: For Freshers & Experienced [2024]
905249
A Software Development Engineer (SDE) is responsible for creating cross-platform applications and software systems, applying the principles of compute
Read More

by Rohan Vats

05 Mar 2024

System Calls in OS: Different types explained
5031
Ever wondered how your computer knows to save a file or display a webpage when you click a button? All thanks to system calls – the secret messengers
Read More

by Prateek Singh

29 Feb 2024

Marquee Tag & Attributes in HTML: Features, Uses, Examples
5168
In my journey as a web developer, one HTML element that has consistently sparked both curiosity and creativity is the venerable Marquee tag. As I delv
Read More

by venkatesh Rajanala

29 Feb 2024

What is Coding? Uses of Coding for Software Engineer in 2024
5068
Introduction  The word “coding” has moved beyond its technical definition in today’s digital age and is now considered an essential ability in
Read More

by Harish K

29 Feb 2024

Functions of Operating System: Features, Uses, Types
5180
The operating system (OS) stands as a crucial component that facilitates the interaction between software and hardware in computer systems. It serves
Read More

by Geetika Mathur

29 Feb 2024

What is Information Technology? Definition and Examples
5087
Information technology includes every digital action that happens within an organization. Everything from running software on your system and organizi
Read More

by spandita hati

29 Feb 2024

50 Networking Interview Questions & Answers (Freshers & Experienced)
5254
In the vast landscape of technology, computer networks serve as the vital infrastructure that underpins modern connectivity.  Understanding the core p
Read More

by Harish K

29 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon