Wrapper Class in Java

Wrapper class in Java is a class whose object wraps a primitive type. When you need an object instead of a primitive type you can use Wrapper class to wrap the primitive type value in an object and use that object.

When is Java Wrapper class needed

Some of the scenarios where you may need a Wrapper class are listed below.

  1. If you have any method that expects an object as argument.
  2. If you want to store primitive types in any data structure which can save only objects for example if you have an array of Objects (Object[] arr) or any of the Java Collections class that can store only objects (List<Integer> numberList).
  3. Wrapper classes also have many other utility methods and fields that you can use if you wrap primitive type into a Wrapper class.
    For example you can use constant fields MIN_VALUE and MAX_VALUE of the Wrapper class to get the upper and lower bounds of the data type.
    You can also use methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems.

Wrapper classes in Java for primitive types

For all eight primitive data types there is a corresponding Wrapper class in Java.

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

Out of these 8 wrapper classes 6 are numeric. All of the numeric wrapper classes are subclasses of the abstract class Number.

wrapper class in Java

Conversion using methods of the Wrapper classes

Though Autoboxing and unboxing (Java 5 onward) can automatically convert primitive into object and object into primitive but you may still use methods of the wrapper classes if you want to convert values to and from other primitive types.
Java Wrapper class examples

1- Converting int to String

2- Converting double to int in Java

3- Converting float to double in Java

4- Wrapping char into a Character Wrapper class. Note that such constructors for all the Wrapper classes are deprecated after Java 8.

Autoboxing and unboxing

Automatic conversion of primitive type to its corresponding Wrapper class is known as Autoboxing. You don’t need to do that conversion automatically.

For example, directly assigning int value to an Integer object. Conversion happens automatically.

Adding int value to an ArrayList that stores Integer object. Autoboxing automatically wraps int value to an Integer object and then it is stored into the List.

Same way Wrapper class object can be converted to corresponding primitive type automatically through Unboxing.

For example- When an Integer stored in a List is retrieved you can assign it to an Integer or to an int too. In that case Conversion from Integer (Wrapper class) to int is done automatically.

That’s all for the topic Wrapper Class in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.