Java Reference
In-Depth Information
Chapter 8
Wrapper Classes
In this chapter, you will learn:
About the wrapper classes and how to use them
How primitive values are automatically boxed into wrapper objects when needed
How wrapper objects are automatically unboxed into primitive values when needed
Wrapper Classes
In previous chapters, you learned that primitive and reference types are not assignment compatible. You cannot even
compare a primitive value with an object reference. Some parts of the Java library work only with objects; for example,
collections in Java work only with objects. You cannot create a list of primitive values, such as 1, 3, 8, and 10. You will
need to wrap the primitive values into objects before you can store them in a list or set.
The assignment incompatibility between primitive values and reference values has existed in Java since its first
release. The Java library provided eight classes in the java.lang package to represent each of the eight primitive types.
These classes are called wrapper classes as they wrap a primitive value in an object. Table 8-1 lists the primitive types
and their corresponding wrapper classes. Notice the names of the wrapper classes. Following the Java convention for
naming classes, they start with an uppercase letter.
Table 8-1. List of Primitive Types and Their Corresponding Wrapper Classes
Primitive Type
Wrapper Class
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
 
Search WWH ::




Custom Search