Java Wrapper Classes

In Java, a wrapper class is a class that encapsulates (wraps) a primitive data type into an object. Since Java is an object-oriented language, primitive types cannot be used as objects. Wrapper classes allow you to use primitive data types like int, char, boolean, etc., as objects.

Wrapper classes are part of the java.lang package, and they provide several important utility methods that facilitate operations such as conversion, comparison, and manipulation of primitive types.

Primitive Types and Wrapper Classes

Primitive Type
Wrapper Class
Example of Wrapper Object
boolean
Boolean
Boolean isTrue = true
byte
Byte
Byte b = 1
char
Character
Character ch = ‘A’
short
Short
Short sh = 100
int
Integer
Integer num = 10
long
Long
Long longNum = 1000L
float
Float
Float f = 3.14f
double
Double
Double d = 99.99

Key Features of Wrapper Classes

1. Autoboxing: Java automatically converts primitive types to their corresponding wrapper class objects (i.e., converting int to Integer).

2. Unboxing: The reverse process, where the wrapper class object is converted back to its primitive type (i.e., converting Integer to int).

3. Utility Methods: Wrapper classes provide static methods to perform operations such as conversion (e.g., parseInt, valueOf), comparison (e.g., compareTo), and getting maximum or minimum values.

4. Nullability: Wrapper classes can be assigned null (unlike primitive types), which is useful in situations like database operations where a field might not be populated.

Autoboxing and Unboxing with Integer

This example demonstrates how Java automatically converts primitive types to wrapper objects (autoboxing) and converts them back to primitives (unboxing).


//WrapperAutoboxingExample.java file
public class WrapperAutoboxingExample {
    public static void main(String[] args) {
        // Autoboxing: Primitive to Wrapper Object
        int primitiveInt = 50;
        Integer wrappedInt = primitiveInt;  // autoboxing
        System.out.println("Autoboxed Integer: " + wrappedInt);

        // Unboxing: Wrapper Object to Primitive
        Integer anotherWrappedInt = new Integer(70);
        int unboxedInt = anotherWrappedInt;  // unboxing
        System.out.println("Unboxed int: " + unboxedInt);
    }
}

Output:

Autoboxed Integer: 50
Unboxed int: 70

Using Wrapper Class Methods (Integer, Double, Character)

Wrapper classes have useful methods for parsing, converting, and comparing values. In this example, I’ll demonstrate parseInt, parseDouble, valueOf, and compareTo methods.


//WrapperMethodsExample.java file
public class WrapperMethodsExample {
    public static void main(String[] args) {
        // Parsing a String to Integer
        String intStr = "50";
        int parsedInt = Integer.parseInt(intStr);
        System.out.println("Parsed Integer from String: " + parsedInt);

        // Parsing a String to Double
        String doubleStr = "3.14159";
        double parsedDouble = Double.parseDouble(doubleStr);
        System.out.println("Parsed Double from String: " + parsedDouble);

        // Using valueOf method
        Integer integerObj = Integer.valueOf(intStr);
        System.out.println("Integer value from valueOf: " + integerObj);

        // Comparing two Integer objects
        Integer a = 10;
        Integer b = 20;
        System.out.println("Comparison result from a to b: " + a.compareTo(b));  // Returns a negative value
        System.out.println("Comparison result from b to a: " + b.compareTo(a));  // Returns a positive value

        // Using wrapper class for Character
        char ch = 'A';
        Character chObj = ch;  // Autoboxing
        System.out.println("Character object: " + chObj);
    }
}

Output:

Parsed Integer from String: 50
Parsed Double from String: 3.14159
Integer value from valueOf: 50
Comparison result from a to b: -1
Comparison result from b to a: 1
Character object: A

Using Wrapper Classes for Nullability (Integer, Double, Boolean)

Wrapper classes can be set to null, which is useful when representing missing or undefined values.


//Main.java file
public class Main {
    public static void main(String[] args) {
        // Nullable Integer
        Integer nullableInt = null;
        if (nullableInt == null) {
            System.out.println("Integer is null");
        }

        // Nullable Double
        Double nullableDouble = null;
        if (nullableDouble == null) {
            System.out.println("Double is null");
        }

        // Nullable Boolean
        Boolean nullableBoolean = null;
        if (nullableBoolean == null) {
            System.out.println("Boolean is null");
        }
    }
}

Output:

Integer is null
Double is null
Boolean is null

Wrapper Class Constants

Each wrapper class has constant values such as MAX_VALUE, MIN_VALUE, etc. This example shows how to use them.


//Main.java file
public class Main {
    public static void main(String[] args) {
        // Maximum and Minimum values for Integer
        System.out.println("Integer MAX_VALUE: " + Integer.MAX_VALUE);
        System.out.println("Integer MIN_VALUE: " + Integer.MIN_VALUE);

        // Maximum and Minimum values for Double
        System.out.println("Double MAX_VALUE: " + Double.MAX_VALUE);
        System.out.println("Double MIN_VALUE: " + Double.MIN_VALUE);
    }
}

Output:

Integer MAX_VALUE: 2147483647
Integer MIN_VALUE: -2147483648
Double MAX_VALUE: 1.7976931348623157E308
Double MIN_VALUE: 4.9E-324

Using Boolean Wrapper Class

The Boolean wrapper class is particularly useful for handling boolean values as objects.


//Main.java file
public class Main {
    public static void main(String[] args) {
        // Autoboxing of boolean primitive
        boolean flag = true;
        Boolean booleanObj = flag;  // Autoboxing
        System.out.println("Boolean Object: " + booleanObj);

        // Parsing a String to Boolean
        String trueStr = "true";
        Boolean parsedBoolean = Boolean.parseBoolean(trueStr);
        System.out.println("Parsed Boolean from String: " + parsedBoolean);

        // Comparing two Boolean objects
        Boolean bool1 = Boolean.TRUE;
        Boolean bool2 = Boolean.FALSE;
        System.out.println("Comparison result: " + bool1.compareTo(bool2));  // Returns 1 because TRUE > FALSE
    }
}

Output:

Boolean Object: true
Parsed Boolean from String: true
Comparison result: 1

Using Float and Long Wrapper Classes

This example demonstrates working with Float and Long wrapper classes, including parsing and utility methods.


//Main.java file
public class Main {
    public static void main(String[] args) {
        // Working with Float
        String floatStr = "3.14";
        Float floatObj = Float.valueOf(floatStr);
        System.out.println("Float Object from String: " + floatObj);

        // Using Float's utility method
        System.out.println("Float MAX_VALUE: " + Float.MAX_VALUE);
        System.out.println("Float MIN_VALUE: " + Float.MIN_VALUE);

        // Working with Long
        String longStr = "1234567890";
        Long longObj = Long.valueOf(longStr);
        System.out.println("Long Object from String: " + longObj);

        // Using Long's utility method
        System.out.println("Long MAX_VALUE: " + Long.MAX_VALUE);
        System.out.println("Long MIN_VALUE: " + Long.MIN_VALUE);
    }
}

Output:

Float Object from String: 3.14
Float MAX_VALUE: 3.4028235E38
Float MIN_VALUE: 1.4E-45
Long Object from String: 1234567890
Long MAX_VALUE: 9223372036854775807
Long MIN_VALUE: -9223372036854775808

Comparing Wrapper Class Objects with equals()

This example demonstrates how to compare two wrapper class objects using the equals() method.


//Main.java file
public class Main {
    public static void main(String[] args) {
        // Comparing Integer objects
        Integer num1 = 10;
        Integer num2 = 20;
        Integer num3 = 20;
        
        System.out.println("num1 equals num2? " + num1.equals(num2));  // true
        System.out.println("num2 equals num3? " + num2.equals(num3));  // false
        
        // Comparing Boolean objects
        Boolean bool1 = Boolean.TRUE;
        Boolean bool2 = Boolean.FALSE;
        System.out.println("bool1 equals bool2? " + bool1.equals(bool2));  // false
    }
}

Output:

num1 equals num2? false
num2 equals num3? true
bool1 equals bool2? false