Java Wrapper Classes

Introduction

Java wrapper classes provide a way to use primitive data types (int, boolean, etc.) as objects. Each of the eight primitive types has a corresponding wrapper class in Java. These classes are part of the java.lang package and provide methods to manipulate primitive data types and convert them to objects.

1. What are Wrapper Classes?

Wrapper classes in Java are used to convert primitive data types into objects. This is useful because many Java libraries and frameworks require objects rather than primitive types. Additionally, wrapper classes provide utility methods for converting between different types, comparing values, and more.

2. Autoboxing and Unboxing

Autoboxing is the automatic conversion of primitive types to their corresponding wrapper class objects. Unboxing is the reverse process, where wrapper class objects are converted back to primitive types.

Example:

public class AutoboxingUnboxingExample {
    public static void main(String[] args) {
        // Autoboxing: primitive to wrapper
        int primitiveInt = 5;
        Integer wrappedInt = primitiveInt; // Integer.valueOf(primitiveInt)

        // Unboxing: wrapper to primitive
        Integer wrappedInteger = new Integer(10);
        int primitiveInt2 = wrappedInteger; // wrappedInteger.intValue()

        System.out.println("Autoboxing: " + wrappedInt);
        System.out.println("Unboxing: " + primitiveInt2);
    }
}

Output:

Autoboxing: 5
Unboxing: 10

3. List of Wrapper Classes

Here are the primitive data types and their corresponding wrapper classes:

Primitive Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

4. Byte Wrapper Class

The Byte wrapper class provides methods to manipulate byte values and convert them to objects.

Example:

public class ByteWrapperExample {
    public static void main(String[] args) {
        // Autoboxing
        Byte byteObject = 10;

        // Unboxing
        byte primitiveByte = byteObject;

        // Methods
        Byte byteValue = Byte.valueOf("20");
        byte parsedByte = Byte.parseByte("30");

        System.out.println("Byte object: " + byteObject);
        System.out.println("Primitive byte: " + primitiveByte);
        System.out.println("Byte value from String: " + byteValue);
        System.out.println("Parsed byte value: " + parsedByte);
    }
}

Output:

Byte object: 10
Primitive byte: 10
Byte value from String: 20
Parsed byte value: 30

5. Short Wrapper Class

The Short wrapper class provides methods to manipulate short values and convert them to objects.

Example:

public class ShortWrapperExample {
    public static void main(String[] args) {
        // Autoboxing
        Short shortObject = 100;

        // Unboxing
        short primitiveShort = shortObject;

        // Methods
        Short shortValue = Short.valueOf("200");
        short parsedShort = Short.parseShort("300");

        System.out.println("Short object: " + shortObject);
        System.out.println("Primitive short: " + primitiveShort);
        System.out.println("Short value from String: " + shortValue);
        System.out.println("Parsed short value: " + parsedShort);
    }
}

Output:

Short object: 100
Primitive short: 100
Short value from String: 200
Parsed short value: 300

6. Integer Wrapper Class

The Integer wrapper class provides methods to manipulate int values and convert them to objects.

Example:

public class IntegerWrapperExample {
    public static void main(String[] args) {
        // Autoboxing
        Integer intObject = 1000;

        // Unboxing
        int primitiveInt = intObject;

        // Methods
        Integer intValue = Integer.valueOf("2000");
        int parsedInt = Integer.parseInt("3000");
        String binaryString = Integer.toBinaryString(10);
        int compareResult = Integer.compare(100, 200);

        System.out.println("Integer object: " + intObject);
        System.out.println("Primitive int: " + primitiveInt);
        System.out.println("Integer value from String: " + intValue);
        System.out.println("Parsed int value: " + parsedInt);
        System.out.println("Binary representation of 10: " + binaryString);
        System.out.println("Comparison result (100 vs 200): " + compareResult);
    }
}

Output:

Integer object: 1000
Primitive int: 1000
Integer value from String: 2000
Parsed int value: 3000
Binary representation of 10: 1010
Comparison result (100 vs 200): -1

7. Long Wrapper Class

The Long wrapper class provides methods to manipulate long values and convert them to objects.

Example:

public class LongWrapperExample {
    public static void main(String[] args) {
        // Autoboxing
        Long longObject = 10000L;

        // Unboxing
        long primitiveLong = longObject;

        // Methods
        Long longValue = Long.valueOf("20000");
        long parsedLong = Long.parseLong("30000");
        String hexString = Long.toHexString(255);
        int compareResult = Long.compare(1000L, 2000L);

        System.out.println("Long object: " + longObject);
        System.out.println("Primitive long: " + primitiveLong);
        System.out.println("Long value from String: " + longValue);
        System.out.println("Parsed long value: " + parsedLong);
        System.out.println("Hex representation of 255: " + hexString);
        System.out.println("Comparison result (1000L vs 2000L): " + compareResult);
    }
}

Output:

Long object: 10000
Primitive long: 10000
Long value from String: 20000
Parsed long value: 30000
Hex representation of 255: ff
Comparison result (1000L vs 2000L): -1

8. Float Wrapper Class

The Float wrapper class provides methods to manipulate float values and convert them to objects.

Example:

public class FloatWrapperExample {
    public static void main(String[] args) {
        // Autoboxing
        Float floatObject = 10.5f;

        // Unboxing
        float primitiveFloat = floatObject;

        // Methods
        Float floatValue = Float.valueOf("20.5");
        float parsedFloat = Float.parseFloat("30.5");
        int compareResult = Float.compare(10.5f, 20.5f);

        System.out.println("Float object: " + floatObject);
        System.out.println("Primitive float: " + primitiveFloat);
        System.out.println("Float value from String: " + floatValue);
        System.out.println("Parsed float value: " + parsedFloat);
        System.out.println("Comparison result (10.5f vs 20.5f): " + compareResult);
    }
}

Output:

Float object: 10.5
Primitive float: 10.5
Float value from String: 20.5
Parsed float value: 30.5
Comparison result (10.5f vs 20.5f): -1

9. Double Wrapper Class

The Double wrapper class provides methods to manipulate double values and convert them to objects.

Example:

public class DoubleWrapperExample {
    public static void main(String[] args) {
        // Autoboxing
        Double doubleObject = 10.55;

        // Unboxing
        double primitiveDouble = doubleObject;

        // Methods
        Double doubleValue = Double.valueOf("20.55");
        double parsedDouble = Double.parseDouble("30.55");
        int compareResult = Double.compare(10.55, 20.55);

        System.out.println("Double object: " + doubleObject);
        System.out.println("Primitive double: " + primitiveDouble);
        System.out.println("Double value from String: " + doubleValue);
        System.out.println("Parsed double value: " + parsedDouble);
        System.out.println("Comparison result (10.55 vs 20.55): " + compareResult);
    }
}

Output:

Double object: 10.55
Primitive double: 10.55
Double value from String: 20.55
Parsed double value: 30.55
Comparison result (10.55 vs 20.55): -1

10. Character Wrapper Class

The Character wrapper class provides methods to manipulate char values and convert them to objects.

Example:

public class CharacterWrapperExample {
    public static void main(String[] args) {
        // Autoboxing
        Character charObject = 'A';

        // Unboxing
        char primitiveChar = charObject;

        // Methods
        boolean isDigit = Character.isDigit('5');
        boolean isLetter = Character.isLetter('A');
        char toLowerCase = Character.toLowerCase('B');

        System.out.println("Character object: " + charObject);
        System.out.println("Primitive char: " + primitiveChar);
        System.out.println("Is '5' a digit? " + isDigit);
        System.out.println("Is 'A' a letter? " + isLetter);
        System.out.println("'B' to lower case: " + toLowerCase);
    }
}

Output:

Character object: A
Primitive char: A
Is '5' a digit? true
Is 'A' a letter? true
'B' to lower case: b

11. Boolean Wrapper Class

The Boolean wrapper class provides methods to manipulate boolean values and convert them to objects.

Example:

public class BooleanWrapperExample {
    public static void main(String[] args) {
        // Autoboxing
        Boolean booleanObject = true;

        // Unboxing
        boolean primitiveBoolean = booleanObject;

        // Methods
        Boolean boolValue = Boolean.valueOf("true");
        boolean parsedBoolean = Boolean.parseBoolean("false");

        System.out.println("Boolean object: " + booleanObject);
        System.out.println("Primitive boolean: " + primitiveBoolean);
        System.out.println("Boolean value from String: " + boolValue);
        System.out.println("Parsed boolean value: " + parsedBoolean);
    }
}

Output:

Boolean object: true
Primitive boolean: true
Boolean value from String: true
Parsed boolean value: false

12. Real-World Analogy

Consider using envelopes to send messages:

  • Primitive data types are like messages written on a piece of paper.
  • Wrapper classes are like envelopes that hold these pieces of paper, making it easier to send (pass around) the messages.

This analogy illustrates how wrapper classes encapsulate primitive data types, allowing them to be treated as objects, which is essential for many operations in Java.

13. Conclusion

Java wrapper classes provide a powerful way to handle primitive data types as objects. They offer utility methods for conversion, comparison, and other operations that are not possible with primitives alone. Understanding and using wrapper classes effectively can enhance your Java programming skills, making your code more flexible and capable of leveraging Java’s extensive libraries and frameworks.

Here is a summary of the examples covered:

  • Byte Wrapper Class: Demonstrates autoboxing, unboxing, and parsing byte values.
  • Short Wrapper Class: Shows how to handle short values with autoboxing, unboxing, and parsing.
  • Integer Wrapper Class: Covers methods like parseInt, toBinaryString, and compare.
  • Long Wrapper Class: Includes parsing, hex conversion, and comparison methods for long values.
  • Float Wrapper Class: Explains handling float values and comparing them.
  • Double Wrapper Class: Provides examples of parsing and comparing double values.
  • Character Wrapper Class: Demonstrates methods like isDigit, isLetter, and toLowerCase.
  • Boolean Wrapper Class: Shows parsing and handling boolean values with methods like parseBoolean and valueOf.

Example: Comprehensive Use of Wrapper Classes

public class WrapperClassesExample {
    public static void main(String[] args) {
        // Byte Example
        Byte byteObject = 10;
        byte primitiveByte = byteObject;
        System.out.println("Byte object: " + byteObject);
        System.out.println("Primitive byte: " + primitiveByte);

        // Short Example
        Short shortObject = 100;
        short primitiveShort = shortObject;
        System.out.println("Short object: " + shortObject);
        System.out.println("Primitive short: " + primitiveShort);

        // Integer Example
        Integer intObject = 1000;
        int primitiveInt = intObject;
        System.out.println("Integer object: " + intObject);
        System.out.println("Primitive int: " + primitiveInt);

        // Long Example
        Long longObject = 10000L;
        long primitiveLong = longObject;
        System.out.println("Long object: " + longObject);
        System.out.println("Primitive long: " + primitiveLong);

        // Float Example
        Float floatObject = 10.5f;
        float primitiveFloat = floatObject;
        System.out.println("Float object: " + floatObject);
        System.out.println("Primitive float: " + primitiveFloat);

        // Double Example
        Double doubleObject = 10.55;
        double primitiveDouble = doubleObject;
        System.out.println("Double object: " + doubleObject);
        System.out.println("Primitive double: " + primitiveDouble);

        // Character Example
        Character charObject = 'A';
        char primitiveChar = charObject;
        System.out.println("Character object: " + charObject);
        System.out.println("Primitive char: " + primitiveChar);

        // Boolean Example
        Boolean booleanObject = true;
        boolean primitiveBoolean = booleanObject;
        System.out.println("Boolean object: " + booleanObject);
        System.out.println("Primitive boolean: " + primitiveBoolean);
    }
}

Output:

Byte object: 10
Primitive byte: 10
Short object: 100
Primitive short: 100
Integer object: 1000
Primitive int: 1000
Long object: 10000
Primitive long: 10000
Float object: 10.5
Primitive float: 10.5
Double object: 10.55
Primitive double: 10.55
Character object: A
Primitive char: A
Boolean object: true
Primitive boolean: true

This comprehensive example consolidates the use of all wrapper classes, demonstrating autoboxing, unboxing, and basic manipulations for each primitive type.

By leveraging wrapper classes, you can handle primitives in a more flexible and object-oriented manner, making your Java programs more robust and capable of utilizing the full power of the Java platform.

Leave a Comment

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

Scroll to Top