Wrapper Classes in Java
Last updated
Was this helpful?
Last updated
Was this helpful?
A Wrapper class is a class whose object wraps or contains a primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store a primitive data types. In other words, we can wrap a primitive value into a wrapper class object.
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.
Automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example -- conversion of int to Integer, long to Long, double to Double etc.
It is just the reverse process of autoboxing. Automatically converting an object of a wrapper class to its corresponding primitive type is known as unboxing. For example -- conversion of Integer to int, Long to long, Double to double etc.
e.g.