June 2018
Beginner
722 pages
18h 47m
English
Boxing a primitive type can be done either automatically (called autoboxing) or explicitly using the valueOf() method available in each wrapper type:
int n = 12;Integer integer = n; //an example of autoboxingSystem.out.println(integer); //prints: 12integer = Integer.valueOf(n);System.out.println(integer); //prints: 12Byte b = Byte.valueOf((byte)n);Short s = Short.valueOf((short)n);Long l = Long.valueOf(n);Float f = Float.valueOf(n);Double d = Double.valueOf(n);
Notice that the input value of the valueOf() method of the Byte and Short wrappers required casting because it was a narrowing of a primitive type, which we discussed in the previous section.
Read now
Unlock full access