OCA Java SE 8 Core Java APIs - Java Wrapper Classes








The following table lists all the wrapper classes along with the constructor for each.

Primitive typeWrapper classExample of constructing
booleanBooleannew Boolean(true)
byteBytenew Byte((byte) 1)
shortShortnew Short((short) 1)
intIntegernew Integer(1)
longLongnew Long(1)
floatFloatnew Float(1.0)
doubleDoublenew Double(1.0)
charCharacternew 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 classConverting String to primitiveConverting String to wrapper class
BooleanBoolean.parseBoolean("true");Boolean.valueOf("TRUE");
ByteByte.parseByte("1");Byte.valueOf("2");
ShortShort.parseShort("1");Short.valueOf("2");
IntegerInteger.parseInt("1");Integer.valueOf("2");
LongLong.parseLong("1");Long.valueOf("2");
FloatFloat.parseFloat("1");Float.valueOf("2.2");
DoubleDouble.parseDouble("1");Double.valueOf("2.2");
CharacterNoneNone




Autoboxing

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.





Note

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.