SrcForge

Lesson 7

Wrapper Class

Java Wrapper Class provides a way to use primitive data types as reference data types. Learn boxing, unboxing, and collections usage.

Lesson content

What is a Wrapper Class in Java?

Java Wrapper Class provides a way to use primitive data types as reference data types. Wrapper classes are useful when working with collections, generics, and methods that require objects instead of primitive values.

In Java, every primitive data type has a corresponding wrapper class. A wrapper class converts primitive values into objects.

Definition List

  • Wrapper Class: A class in Java that wraps a primitive data type into an object.
  • Primitive Data Type: Basic data types in Java such as int, double, char, and boolean.
  • Wrapping (Boxing): The process of converting a primitive value into a wrapper object.
  • Unwrapping (Unboxing): The process of converting a wrapper object back into a primitive value.

Why Use Wrapper Classes in Java?

Wrapper classes are commonly used because:

  • Java Collections Framework works only with objects
  • Generics require reference types
  • Utility methods are available in wrapper classes
  • Wrapper objects can store null values

Common Java Wrapper Classes

Primitive Type → Wrapper Class

  • int → Integer
  • double → Double
  • char → Character
  • boolean → Boolean
  • byte → Byte
  • short → Short
  • long → Long
  • float → Float

Sample Program of Wrapper Class

public class WrapperClassExample {

    public static void main(String[] args) {

        // Primitive data types

        int intValue = 42;

        double doubleValue = 3.14;

        char charValue = 'A';



        // Using wrapper classes

        Integer integerObject = Integer.valueOf(intValue); // Wrapping int

        Double doubleObject = Double.valueOf(doubleValue); // Wrapping double

        Character charObject = Character.valueOf(charValue); // Wrapping char



        System.out.println("Primitive Values:");

        System.out.println("int: " + intValue);

        System.out.println("double: " + doubleValue);

        System.out.println("char: " + charValue);



        System.out.println("\nWrapper Class Values:");

        System.out.println("Integer: " + integerObject);

        System.out.println("Double: " + doubleObject);

        System.out.println("Character: " + charObject);



        // Unwrapping - getting primitive values back

        int unwrappedInt = integerObject.intValue();

        double unwrappedDouble = doubleObject.doubleValue();

        char unwrappedChar = charObject.charValue();



        System.out.println("\nUnwrapped Values:");

        System.out.println("Unwrapped int: " + unwrappedInt);

        System.out.println("Unwrapped double: " + unwrappedDouble);

        System.out.println("Unwrapped char: " + unwrappedChar);

    }
}

Explanation of the Program

Primitive Values

The program first creates primitive variables: intValue, doubleValue, and charValue. These variables store normal primitive data.

Wrapping Primitive Data

The primitive values are converted into wrapper objects using Integer.valueOf(), Double.valueOf(), and Character.valueOf(). This process is called wrapping or boxing.

Printing Wrapper Objects

The wrapper objects are displayed using System.out.println().

Unwrapping Wrapper Objects

The wrapper objects are converted back into primitive values using intValue(), doubleValue(), and charValue(). This process is called unboxing.

Advantages of Wrapper Classes

Easy Conversion

Wrapper classes make it simple to convert primitive values into objects.

Useful Utility Methods

Wrapper classes provide methods for parsing and conversions. Example: Integer.parseInt("100");

Required for Collections

Collections like ArrayList store objects, not primitive values. Example: ArrayList<Integer> numbers = new ArrayList<>();

FAQ

1. What is the purpose of wrapper classes in Java?

Wrapper classes allow primitive data types to be used as objects. They are mainly used in collections, generics, and utility methods.

2. What is the difference between primitive types and wrapper classes?

Primitive types store simple values directly, while wrapper classes store values as objects with additional methods and features.

3. What are boxing and unboxing in Java?

Boxing converts a primitive type into a wrapper object, while unboxing converts a wrapper object back into a primitive type.