EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Wrapper Classes

The first question that typically comes to mind is, “When we already have primitive data types, why do we need wrapper classes in Java?” The reason lies in the additional functionality provided by wrapper classes that primitive data types lack. These features primarily include useful methods like valueOf()parseInt()toString(), and more.

A wrapper class “wraps” around a primitive data type, giving it an object representation. These classes are final and immutable. Two key concepts related to wrapper classes are autoboxing and unboxing.

1. Autoboxing is the automatic conversion of a primitive value into an instance of its corresponding wrapper class (e.g., converting an int to an Integer). The Java compiler applies autoboxing when:

  • A primitive value is passed as an argument to a method that expects an object of the corresponding wrapper class.
  • A primitive value is assigned to a variable of the corresponding wrapper class.

2. Unboxing is the automatic conversion of an instance of a wrapper class to its corresponding primitive value (e.g., converting an Integer to an int). The Java compiler applies unboxing when:

  • An object of a wrapper class is passed as an argument to a method that expects a primitive type.
  • An object of a wrapper class is assigned to a variable of the corresponding primitive type.

Features of Wrapper Classes

Some of the notable benefits of wrapper classes include:

1. They allow conversion between primitive data types and objects. This is useful in cases where arguments passed to methods need to be modified, as primitives are passed by value.
2. Java classes, such as those in the java.util package, deal with objects rather than primitive types, making wrapper classes essential.
3. Data structures in the Collection Framework (e.g., ArrayList, Vector) store only objects, not primitives.
4. Wrapper classes enable object creation for synchronization in multithreading.
They provide numerous utility methods. For instance, a float value can be converted to its integer equivalent using the provided method.

Example 1: Autoboxing in Java

The following code demonstrates autoboxing, where primitive types are automatically converted to their corresponding wrapper classes:

import java.util.*;

class WrapperDemo {

    public static void main(String[] args) {
        int x = 10;
        double y = 7.25;
        long z = 12345;

        // Autoboxing: converting primitives to objects
        Integer intObj = x;
        Double doubleObj = y;
        Long longObj = z;

        // Output the wrapped values
        System.out.println(intObj);
        System.out.println(doubleObj);
        System.out.println(longObj);
    }
}

Output:

10
7.25
12345

Example 2: Wrapper Class Utility Methods

Here’s another example that illustrates the utility methods provided by wrapper classes:

import java.io.*;

class WrapperUtility {

    public static void main(String[] args) {

        // Converting a float to int using a wrapper class method
        Float floatValue = Float.valueOf(28.97f);
        int convertedValue = floatValue.intValue();

        // Output the converted value
        System.out.println(convertedValue);

        // Converting a binary string to an integer
        Integer binaryValue = Integer.valueOf("1101", 2);

        // Output the converted integer from binary
        System.out.println(binaryValue);
    }
}

Output:

28
13
End of lesson.