What is Wrapper Class in Java?

Wrapper classes converts Java primitives data type into the reference types (objects). These are known as wrapper classes because they wrap the primitive data type into an object of that class. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types.

Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object into primitive automatically. The automatic conversion of primitive into object is known as autoboxing and vice-versa unboxing.

The eight classes of java.lang package are known as wrapper classes in java. The list of eight wrapper classes are given below:

Primitive Type Wrapper Class
boolean Boolean
char Char
byte Byte
short Short
int Integer
long Long
float Float
double Double

Need of Wrapper Classes

  • They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).
  • The classes in java.util package handles only objects and hence wrapper classes help in this case also.
  • Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
  • An object is needed to support synchronization in multithreading.

Comments