Each primitive type has a wrapper class in Java, which is an object type that corresponds to the primitive
Primitive Type | Wrapper Class | Example |
---|---|---|
byte | Byte | Byte byteValue = Byte.valueOf((byte) 1); |
short | Short | Short shortValue = Short.valueOf((short) 1); |
int | Integer | Integer intValue = Integer.valueOf(1); |
long | Long | Long longValue = Long.valueOf(1L); |
float | Float | Float floatValue = Float.valueOf(1.0f); |
double | Double | Double doubleValue = Double.valueOf(1.0); |
char | Character | Character charValue = Character.valueOf('A'); |
boolean | Boolean | Boolean 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 Class | String to Primitive Conversion | String to Wrapper Class Conversion |
---|---|---|
Byte | Byte.parseByte(“123”) | Byte.valueOf(“123”) |
Short | Short.parseShort(“123”) | Short.valueOf(“123”) |
Integer | Integer.parseInt(“123”) | Integer.valueOf(“123”) |
Long | Long.parseLong(“123”) | Long.valueOf(“123”) |
Float | Float.parseFloat(“123.45”) | Float.valueOf(“123.45”) |
Double | Double.parseDouble(“123.45”) | Double.valueOf(“123.45”) |
Character | Not Applicable | Character.valueOf(‘A’) |
Boolean | Not Applicable | Boolean.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.