Java Reference
In-Depth Information
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 fa-
miliarity 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 sep-
aration, the Java programming language provides a wrapper class cor-
responding to each of the primitive types. Instances of a given wrapper
class contain a value of the corresponding primitive type. The type hier-
archy 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.
 
Search WWH ::




Custom Search