Wrapper Classes in Java

Wrapper Classes in Java with Examples

In this article, I am going to discuss Wrapper Classes in Java with Examples. Please read our previous article, where we discussed Inner Classes in Java. At the end of this article, you will understand what are wrapper classes and when and how to use this in Java Applications.

What are Wrapper Classes in Java?

Wrapper classes are Java predefined classes that are responsible to convert the given string type the numerical value into equivalent primitive data type and vice-versa. A wrapper class is bundled default with the Java library and it is located in (jre/lib/rt.jar file). When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types. Some of the wrapper classes equivalent to primitive data types are as follows:

What are Wrapper Classes in Java.

The above wrapper classes contain a common static method le: parseXX ();

This method converts the given string type the numerical value into equivalent primitive data type value. The wrapper class is mainly an object which makes the code fully object-oriented.

Need for Wrapper Classes:
  1. The wrapper objects hold much more memory compared to primitive types. 
  2. Wrapper Class will convert primitive data types into objects. The object is needed to support synchronization in multithreading.
  3. Wrapper class objects allow null values while primitive data type doesn’t allow it.
Example to Understand Wrapper Classes in Java:
public class wrapperdemo
{
    public static void main (String[]args)
    {
        String i = "10";
        int k = Integer.parseInt (i);
        double m = Double.parseDouble ("10");
        System.out.println (k);
        System.out.println (m);
        boolean status = Boolean.parseBoolean ("20");
        System.out.println (status);
    }
}
Output:

10
10.0
false

Note: The Wrapper class accepts only string numeric values and the Boolean wrapper class is added from version1.5 Java.

How to Implement a Wrapper Class in Java?

Wrapper class can be implemented in Java in the following two ways:

  1. Autoboxing
  2. Unboxing

In general, autoboxing and unboxing take place whenever a conversion into an object or from an object is required. Thus, autoboxing/ unboxing might occur when an argument is passed to a method, or when a value is returned by a method.

Autoboxing in Java:

“Boxing” refers to converting a primitive value into a corresponding wrapper object. Because this can happen automatically, it’s known as autoboxing. In addition to the simple case of assignments, autoboxing automatically occurs whenever a primitive type must be converted into an object.

With autoboxing, it is no longer necessary to manually construct an object in order to wrap a primitive type. You need only assign that value to a type-wrapper reference. Java automatically constructs the object for you.

Example for Autoboxing in Java:
public class AutoBoxing
{
    public static void main (String args[])
    {
        int a = 100;	// Primitive data type
        Integer I = a;	// Autoboxing will occur internally.
    }
}
Unboxing in Java:

Auto-unboxing takes place whenever an object must be converted into a primitive type. Auto-unboxing is the process by which the value of a boxed object is automatically extracted (unboxed) from a type wrapper when its value is needed. There is no need to call a method such as intValue( ) or doubleValue( ).

Example of Unboxing in Java:
public class AutoBoxing
{
    public static void main (String args[])
    {
        Integer a = new Integer (15);	// Wrapper class object
        int I = a;			// Unboxing will occur internally.
    }
}
Need for Autoboxing and Unboxing in Java:
  • The addition of autoboxing and auto-unboxing greatly streamlines the coding of several algorithms, removing the tedium of manually boxing and unboxing values.
  • It also helps prevent errors.
  • Moreover, it is very important to generics, which operate only on objects.

In the next article, I am going to discuss Polymorphism in Java with Examples. Here, in this article, I try to explain the Wrapper Classes in Java with Examples and I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.