The following table lists all the wrapper classes along with the constructor for each.
Primitive type | Wrapper class | Example of constructing |
---|---|---|
boolean | Boolean | new Boolean(true) |
byte | Byte | new Byte((byte) 1) |
short | Short | new Short((short) 1) |
int | Integer | new Integer(1) |
long | Long | new Long(1) |
float | Float | new Float(1.0) |
double | Double | new Double(1.0) |
char | Character | new Character('c') |
The wrapper classes have a method that converts back to a primitive.
There are methods for converting a String to a primitive or wrapper class.
The parse methods, such as parseInt(), return a primitive, and the valueOf() method returns a wrapper class.
For example:
//To convert a String to an int primitive. int primitive = Integer.parseInt("123"); //To convert a String to an Integer wrapper class. Integer wrapper = Integer.valueOf("123");
If the String passed in is not valid for the given type, Java throws an exception.
int bad1 = Integer.parseInt("a"); // throws NumberFormatException Integer bad2 = Integer.valueOf("123.45"); // throws NumberFormatException
The following table lists the methods for creating a primitive or wrapper class object from a String.
Wrapper class | Converting String to primitive | Converting String to wrapper class |
---|---|---|
Boolean | Boolean.parseBoolean("true"); | Boolean.valueOf("TRUE"); |
Byte | Byte.parseByte("1"); | Byte.valueOf("2"); |
Short | Short.parseShort("1"); | Short.valueOf("2"); |
Integer | Integer.parseInt("1"); | Integer.valueOf("2"); |
Long | Long.parseLong("1"); | Long.valueOf("2"); |
Float | Float.parseFloat("1"); | Float.valueOf("2.2"); |
Double | Double.parseDouble("1"); | Double.valueOf("2.2"); |
Character | None | None |
You can type the primitive value and Java will convert it to the relevant wrapper class for you.
This is called autoboxing.
Let's look at an example:
List<Double> weights = new ArrayList<>(); //autoboxes the double primitive into a //Double object and adds that to the List. weights.add(50.5); // [50.5] //write code the long way and pass in a wrapper object. weights.add(new Double(60)); // [50.5, 60.0] //autoboxes into the wrapper object and passes it to remove(). weights.remove(50.5); // [60.0] //retrieves the Double and unboxes it into a double primitive. double first = weights.get(0); // 60.0
Java throws exceptions if you try to unbox a null.
1: List<Integer> heights = new ArrayList<>();
2: heights.add(null);
3: int h = heights.get(0); // NullPointerException
On line 2, we add a null to the list.
This is legal because a null reference can be assigned to any reference variable.
On line 3, we try to unbox that null to an int primitive. Java tries to get the int value of null.
Calling any method on null gives a NullPointerException.
Be careful when autoboxing into Integer.
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.remove(1);
System.out.println(numbers);
After adding the two values, the List contains [1, 2]. We request the element with index 1 be removed. Not the value 1. remove() method takes an int parameter, Java calls that method rather than autoboxing.
If you want to remove the 2, you can write numbers.remove(new Integer(2)) to force wrapper class use.