Explain with examples: Autoboxing and Unboxing, Type Wrapper

1.B] Explain with examples:
i) Autoboxing and Unboxing
ii) Type Wrapper

Answer:

i) Autoboxing and Unboxing

Autoboxing is the automatic conversion that the Java compiler makes between the primitive data types (e.g., int, char, boolean, etc.) and their corresponding object wrapper classes (e.g., Integer, Character, Boolean, etc.).

Unboxing is the reverse process, where the object wrapper class is automatically converted back to the corresponding primitive type.

Example of Autoboxing:

public class AutoboxingExample {
    public static void main(String[] args) {
        // Autoboxing: primitive int is converted to Integer object
        int num = 100;
        Integer boxedNum = num; // Autoboxing
        System.out.println("Boxed Integer: " + boxedNum);
    }
}

In this example, the primitive int value 100 is automatically converted to an Integer object by the compiler.

Example of Unboxing:

public class UnboxingExample {
    public static void main(String[] args) {
        // Unboxing: Integer object is converted to primitive int
        Integer boxedNum = 200;
        int num = boxedNum; // Unboxing
        System.out.println("Unboxed int: " + num);
    }
}

In this example, the Integer object boxedNum is automatically converted back to the primitive int value 200.

Key Points:

  • Autoboxing: int num = 100; Integer boxedNum = num;
  • Unboxing: Integer boxedNum = 200; int num = boxedNum;

ii) Type Wrapper

In Java, primitive data types (e.g., int, char, boolean) have corresponding object wrapper classes, collectively known as “Type Wrappers.” These wrapper classes are part of the java.lang package and provide a way to treat primitive values as objects.

Common Wrapper Classes:

  • intInteger
  • charCharacter
  • booleanBoolean
  • floatFloat
  • doubleDouble

Example of Using Type Wrapper:

public class WrapperExample {
    public static void main(String[] args) {
        // Create an Integer object using the Integer wrapper class
        Integer numObject = Integer.valueOf(10); // Boxing manually
        System.out.println("Integer object: " + numObject);

        // Convert Integer object back to primitive int
        int numPrimitive = numObject.intValue(); // Unboxing manually
        System.out.println("Primitive int: " + numPrimitive);

        // Use of utility methods from wrapper class
        String binaryString = Integer.toBinaryString(numPrimitive);
        System.out.println("Binary representation: " + binaryString);
    }
}

Explanation:

  • Boxing: Integer numObject = Integer.valueOf(10); – The primitive int value 10 is manually converted to an Integer object.
  • Unboxing: int numPrimitive = numObject.intValue(); – The Integer object is manually converted back to a primitive int.
  • Utility Methods: Wrapper classes provide useful methods like toBinaryString(), parseInt(), valueOf(), etc., which are not available for primitive types.

Summary:

  • Autoboxing: Automatic conversion from primitive type to its wrapper class (e.g., int to Integer).
  • Unboxing: Automatic conversion from wrapper class to its primitive type (e.g., Integer to int).
  • Type Wrapper: Classes that encapsulate primitive data types as objects, providing additional methods and utilities for manipulating and converting these types.

Leave a Reply

Your email address will not be published. Required fields are marked *