Wrapper Classes in Java

Introduction

In this lesson of our Java course we will learn about the wrapper classes in Java.Wrapper classes in Java provide a way to convert primitive data types into objects, so that they can be used in object-oriented programming. In this article, we will discuss what wrapper classes are, why they are used, and how they are used in Java.

1. What are Wrapper Classes in Java?

Wrapper classes in Java are classes that provide a way to wrap primitive data types in an object, so that they can be used in an object-oriented context. There is a wrapper class for each of the eight primitive data types in Java. These classes are:

Primitive TypeWrapper Class
booleanBoolean
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
Wrapper Classes in Java
Wrapper Classes in Java

Primitive types are basic data types that are not objects, while wrapper classes provide a way to use primitive types as objects in Java. The wrapper classes provide useful methods for converting between primitive types and objects, as well as for performing various operations on the values they represent.

Each of these classes is a sub-class of the Object class and contains a single field of the corresponding primitive data type. The primary purpose of wrapper classes is to provide a way to convert primitive data types into objects, so that they can be used in an object-oriented context.

2. How to Use Wrapper Classes in Java

Wrapper classes in Java can be used in several ways, including:

2.1. Creating Wrapper Objects

Wrapper objects can be created by calling the constructor of the wrapper class and passing the primitive data type value as an argument. For example:

Integer myInt = new Integer(42);

This code creates a new Integer object with the value 42.

2.2. Converting Primitive Data Types to Wrapper Objects

Primitive data types can be converted to wrapper objects by using the valueOf() method of the wrapper class. For example:

Integer myInt = Integer.valueOf(42);

This code converts the primitive int value 42 to an Integer object.

2.3. Converting Wrapper Objects to Primitive Data Types

Wrapper objects can be converted to primitive data types by using the xxxValue() method of the wrapper class, where “xxx” is the name of the primitive data type. For example:

int myInt = myInteger.intValue();

This code converts the Integer object myInteger to a primitive int value.

2.4. Autoboxing and Unboxing

Autoboxing is the automatic conversion of a primitive data type to its corresponding wrapper class. Unboxing is the automatic conversion of a wrapper object to its corresponding primitive data type. For example:

Integer myInt = 42

2.5. Autoboxing

Integer myInt = 42;

In this code, the primitive int value 42 is automatically converted to an Integer object using autoboxing.

2.6. Unboxing

int myInt = myInteger;

In this code, the Integer object myInteger is automatically converted to a primitive int value using unboxing.

3. Comparison

Wrapper objects can be compared using the equals() method or the “==” operator. For example:

Integer myInt1 = new Integer(42);
Integer myInt2 = new Integer(42);

if (myInt1.equals(myInt2)) {
    System.out.println("myInt1 and myInt2 are equal.");
}

if (myInt1 == myInt2) {
    System.out.println("myInt1 and myInt2 are the same object.");
}

In this code, the equals() method is used to compare the values of the two Integer objects, and the "==" operator is used to compare their references.

3. Examples:

3.1. Converting Primitive types to Wrapper Classes

public class WrapperClassExample {
    public static void main(String[] args) {
        // Creating a wrapper class for an int primitive type
        Integer myInt = Integer.valueOf(10); // Using the valueOf() method
        int myPrimitiveInt = myInt.intValue(); // Using the intValue() method
        System.out.println("My Integer value is: " + myInt);
        System.out.println("My primitive int value is: " + myPrimitiveInt);
        
        // Creating a wrapper class for a boolean primitive type
        Boolean myBoolean = Boolean.valueOf(true); // Using the valueOf() method
        boolean myPrimitiveBoolean = myBoolean.booleanValue(); // Using the booleanValue() method
        System.out.println("My Boolean value is: " + myBoolean);
        System.out.println("My primitive boolean value is: " + myPrimitiveBoolean);
    }
}

In this example, we create a wrapper class for an int primitive type and a boolean primitive type using the Integer and Boolean classes, respectively. We then use the valueOf() method to create an object of the corresponding wrapper class, passing the primitive value as an argument. We can then use various methods of the wrapper class to manipulate the value, such as intValue() and booleanValue(), to extract the primitive value from the wrapper class object.

3.2. Converting Wrapper Classes to Primitive Types

public class WrapperToPrimitiveExample {
    public static void main(String[] args) {
        // Creating a wrapper class for an int primitive type
        Integer myInt = Integer.valueOf(10);
        
        // Converting wrapper class to primitive type
        int myPrimitiveInt = myInt.intValue();
        System.out.println("My primitive int value is: " + myPrimitiveInt);
        
        // Creating a wrapper class for a boolean primitive type
        Boolean myBoolean = Boolean.valueOf(true);
        
        // Converting wrapper class to primitive type
        boolean myPrimitiveBoolean = myBoolean.booleanValue();
        System.out.println("My primitive boolean value is: " + myPrimitiveBoolean);
    }
}

In this example, we first create wrapper class objects myInt and myBoolean for the int and boolean primitive types, respectively, using the valueOf() method. We then convert these wrapper classes back to their corresponding primitive types using the intValue() and booleanValue() methods, respectively, and store the values in myPrimitiveInt and myPrimitiveBoolean. We can then use these primitive values in the same way we would use any other primitive values.

Summary

Wrapper classes in Java provide a way to convert primitive data types into objects, so that they can be used in an object-oriented context. They are used in Java for several reasons, including object-oriented programming, generic data structures, polymorphism, and the Collections Framework.

Wrapper classes can be used in Java to create wrapper objects, convert primitive data types to wrapper objects, convert wrapper objects to primitive data types, perform autoboxing and unboxing, and compare wrapper objects. Knowing how to use wrapper classes in Java is essential for writing effective and efficient Java code. The source code for this course in available on our GitHub repository.

Scroll to Top