Again, hiding of type names is possible, but a type can always be explicitly referred to by its fully qualified
name, which includes package information, such as java.lang.String. Packages and type imports are
discussed in Chapter 18.
In order to make an apple pie from scratch, you must first create the universe.
Carl Sagan, Cosmos
Chapter 8. Primitives as Types
I'm gonna wrap myself in paper, I'm gonna dab myself with glue, Stick some stamps on top of
my head! I'm gonna mail myself to you.
Woody Guthrie, Mail Myself to You
The separation of primitive types (byte, float, and so on) and reference types (classes and interfaces) is a
trade-off between efficiency and familiarity versus expressiveness and consistency. An object may incur too
much overhead where a plain int will do, while an int may be fast and convenient until you need to store it
into a hashtable. To smooth this separation, the Java programming language provides a wrapper class
corresponding to each of the primitive types. Instances of a given wrapper class contain a value of the
corresponding primitive type. The type hierarchy for these classes looks like this:
The language provides automatic conversion between primitives and their wrappers in many contexts,
specifically when assigning values or passing arguments. For example, you can write:
Integer val = 3;
And val will be assigned a reference to an instance of the Integer class that holds the value 3.
Converting a primitive value to a wrapper object is known as a boxing conversion; unsurprisingly, extracting
a primitive value from a wrapper object is known as an unboxing conversion.[1] The details of these
conversions are discussed in "Boxing Conversions" on page 198. These automated conversions make it easy
to write general purposes classes that are written in terms of Object references, but that can handle either
reference types or primitive types. The HashMap class, for example, stores only references, not primitive
typessee "HashMap" on page 590but an int can be passed as a key because it will be converted to an
Integer instance:
[1] This difference in terminology ("wrap" versus "box") is an historical artifact.
int key = ... ;
map.put(key, value);