Wrapper Classes in Java — How they Work with Examples

In Java, we have primitive data types such as long, short, Boolean, int, float and so on.

You can use wrapper classes to convert primitive data types to objects like this:

public static void Calculation()
{
int a = 2;
Integer obj = new Integer(a);
System.out.println(obj.toString());
}

In this code, int is a primitive data type. If we want to perform any operation like a.someMethod() on an integer type, we can't. To perform any operation we must have an object instantiation.

A wrapper class is a class in Java that takes up a primitive data type and then does some operations using that primitive data type.

This class has some methods attached to it. For example, the integer wrapper class used in the above code will return the string data type of integer type.

For that we are initiating the integer wrapper class like this:

Integer obj = new Integer(a)

Why Should You Convert Primitive Data Types to Objects in Java?

  • Collection (a data structure in Java) Frameworks in Java stores objects only. Collections do not support primitive data types.
  • Objects are needed for multi-threading synchronization.
  • Wrapper class methods allow you to convert one data type to another data type.
  • Java Generics work only with object types and don’t support primitive types.

Here’s a table showing the primitive data types we use in Java and their corresponding wrapper classes:

Float fl1 = new Float(25.3F)

What is Autoboxing?

Autoboxing automatically converts primitive types into their corresponding wrapper classes.

Integer k = 41
Float m = 8.2F

Directly assigning like in the above example will be automatically converted to an object.

What is Unboxing?

Unboxing is the reverse process of autoboxing. It lets you convert a wrapper class to its corresponding primitive data type.

public class UnboxingExmp
{
public static void main(String args[])
{
Integer a = new Integer(40);
System.out.println(a/2) // automatically unboxing happens and prints 20
}
}

Note: Here when we take object a, the a represents the reference for the memory heap. When we do the operation on a, it will automatically convert to its primitive data type and return the value.

This automatic conversion from wrapper class object to primitive data type is called Unboxing.

Let’s see the following code block to see how we can get the input as a string and convert it to an integer:

public class StrToInt
{
public static void main(String args[])
{
Scanner in1 = new Scanner(System.in);
System.out.println("Enter the number:");
string num1 = input.next();
int a = Integer.parseInt(num); // converting string num variable to String
System.ot.println(a)
}
}

parseInt is a static method in the integer wrapper class that converts string data type to integer data type.

You might have experience with getting the input values from forms. Sometimes the values from the form might be strings. In those cases, you can do data type conversion by using a wrapper class.

Thanks for reading!

I hope you now have an idea about what wrapper classes in Java are and how to use them.

Connect with me on Twitter for more updates

--

--

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