You are currently viewing Wrapper Classes
java_logo

Wrapper Classes

Each primitive type has a wrapper class in Java, which is an object type that corresponds to the primitive

Primitive TypeWrapper ClassExample
byteByteByte byteValue = Byte.valueOf((byte) 1);
shortShortShort shortValue = Short.valueOf((short) 1);
intIntegerInteger intValue = Integer.valueOf(1);
longLongLong longValue = Long.valueOf(1L);
floatFloatFloat floatValue = Float.valueOf(1.0f);
doubleDoubleDouble doubleValue = Double.valueOf(1.0);
charCharacterCharacter charValue = Character.valueOf('A');
booleanBooleanBoolean boolValue = Boolean.valueOf(true);

Wrapper classes provide methods to convert their object representation to primitive types using methods like intValue(), doubleValue(), etc.

Here’s an example for Integer:

public class WrapperToPrimitiveExample {
    public static void main(String[] args) {
        Integer integerObject = Integer.valueOf(42);

        // Converting Integer to int using intValue()
        int primitiveInt = integerObject.intValue();
    }
}

In this example, intValue() is called on the Integer object to convert it to an int.

   int bad = Integer.parseInt("bad"); // NumberFormatException

Similarly, you can use corresponding methods for other primitive types like doubleValue() for Double, floatValue() for Float, etc. However, the need for these methods has removed since autoboxing (see next chapter)

There are also methods for converting a String to a primitive:

Wrapper ClassString to Primitive ConversionString to Wrapper Class Conversion
ByteByte.parseByte(“123”)Byte.valueOf(“123”)
ShortShort.parseShort(“123”)Short.valueOf(“123”)
IntegerInteger.parseInt(“123”)Integer.valueOf(“123”)
LongLong.parseLong(“123”)Long.valueOf(“123”)
FloatFloat.parseFloat(“123.45”)Float.valueOf(“123.45”)
DoubleDouble.parseDouble(“123.45”)Double.valueOf(“123.45”)
CharacterNot ApplicableCharacter.valueOf(‘A’)
BooleanNot ApplicableBoolean.valueOf(“true”)

AutoBoxing and Unboxing

Autoboxing and unboxing are features in Java that automatically convert between primitive types and their corresponding wrapper classes, making it convenient for developers when dealing with collections or methods that expect objects.

// Autoboxing: converting primitive int to Integer
int primitiveInt = 42;
Integer wrapperInt = primitiveInt; // Autoboxing
int unboxedInt = wrapperInt; // Unboxing

System.out.println(wrapperInt); // Output: 42
System.out.println(unboxedInt); // Output: 42

Autoboxing and unboxing are especially useful when working with collections like ArrayList or when calling methods that expect objects. They help simplify code and make it more readable.

// Autoboxing in a collection
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);  // Autoboxing

// Unboxing from a collection
int firstNumber = numbers.get(0);  // Unboxing

With autoboxing and unboxing, developers can seamlessly work with both primitive types and their corresponding wrapper classes, improving code clarity and reducing the need for manual conversions.

Attempting to unbox a null wrapper object results in a NullPointerException. This is because there is no value to extract from a null reference.

public class UnboxingExample {
    public static void main(String[] args) {
        Integer wrapperInt = null;

        try {
            int primitiveInt = wrapperInt; // Unboxing with null
        } catch (NullPointerException e) {
            System.out.println("NullPointerException")
        }
    }
}

In this example, when trying to unbox a null Integer (wrapperInt), a NullPointerException is thrown. It’s essential to ensure that a wrapper object is non-null before attempting to unbox it to avoid runtime exceptions.