Wrapper Classes in Java

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.

To get in-Depth knowledge on Java you can enroll for a live demo on Java Online Training

Need of Wrapper Classes

  1. 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).
  2. The classes in java.util package handles only objects and hence wrapper classes help in this case also.
  3. Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
  4. An object is needed to support synchronization in multithreading.

Primitive Data types and their Corresponding Wrapper class

Autoboxing and Unboxing

Autoboxing: 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.

Take your career to new heights of success with Java Training
Example:

Output:

Unboxing: 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.

Output:

Implementation

Output:

To learn more about Wrapper Classes in Java and other great features of Java, you can enroll for a live demo on Core Java Online Training

--

--

digital marketer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store