Java Reference
In-Depth Information
These objectives are Section 3 of the SCJP exam objectives. The
exam tests your knowledge of the primitive wrapper classes,
input and output streams, the
java.text
package, formatting
and parsing data using locales, and regular expressions. This
chapter covers these topics in detail, starting with a discussion of the wrapper classes.
The Primitive Wrapper Classes
Data in a Java application is either an object or a primitive data type. There are situations
in Java where only an object can be used and primitive types do not work. For example, the
classes in the Collections API can only hold
Object
references. The wrapper classes provide
the ability to treat primitive types as objects by wrapping the primitive in an
Object
. This
section discusses how to wrap a primitive type into its corresponding wrapper class.
The exam objectives state that you should be able to “develop code that uses the primitive
wrapper classes.” The
wrapper classes
are defi ned in the
java.lang
package and are used in
situations where an object is required but the data is a primitive type. The primitive type is
“wrapped” into an object and can be “unwrapped” whenever the primitive value is needed.
There is a wrapper class in the
java.lang
package for each of the eight primitive types:
Byte
This type wraps a
byte.
Short
This type wraps a
short
.
Integer
This type wraps an
int
.
Long
This type wraps a
long
.
Float
This type wraps a
float
.
Double
This type wraps a
double
.
Character
This type wraps a
char
.
Boolean
This type wraps a
boolean.
Wrapper classes have the following properties:
Each of the wrapper classes contains a single field that holds the value it is wrapping.
The value of the wrapped primitive type cannot be changed.
Each class has a constructor that takes in the data type it wraps.
Except for
Character
, each class has a constructor that takes in a
String
that is
automatically parsed into the corresponding primitive type.
Search WWH ::
Custom Search