- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the need of wrapper classes in Java?
In java.lang package you can find a set of classes which wraps a primitive variable with in their object these are known as wrapper classes. Following is the list of primitive datatypes and their respective classes −
Primitive datatype | Wrapper class |
---|---|
char | Character |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
Example
public class Sample { public static void main (String args[]){ Integer obj = new Integer("2526"); int i = obj.intValue(); System.out.println(i); } }
Output
2526
Need for wrapper classes in Java
Java provides primitive datatypes (char, byte, short, int, long, float, double, boolean) and, reference types to store values.
- Whenever we pass primitive datatypes to a method the value of those will be passed instead of the reference therefore you cannot modify the arguments we pass to the methods. In this scenario you can use objects of such variables.
- Classes in certain packages like util handles only objects.
- Collection types such as ArrayList, Vectors etc. stores only objects (not primitive datatypes).
- Your data need to be an objects for synchronization, serialization and, multithreading.
Therefore, at times we need to have primitive datatypes in the form of objects. At such scenarios you can use wrapper classes.
- Related Articles
- What are wrapper classes in Java?
- Explain wrapper classes in Java?
- Primitive Wrapper Classes are Immutable in Java
- Why do we need a wrapper class in Java?
- Why do we need inner classes in Java?
- What is the number wrapper class and its methods in Java?
- What is the character wrapper class and its methods in Java?
- What is the difference between static classes and non-static inner classes in Java?
- What is the use of FileInputStream and FileOutputStream in classes in Java?
- What are the different types of classes in Java?
- Java Float Wrapper Class
- What is the difference between interfaces and abstract classes in Java?
- What are Java classes?
- What is a Stream and what are the types of Streams and classes in Java?
- What are final classes in Java?

Advertisements