Java Reference
In-Depth Information
algorithm works to perform its task—based on the number of data items to be processed.
After reading Chapter 19, you'll better understand each collection's performance charac-
teristics as described in the online documentation.
Each primitive type (listed in Appendix D) has a corresponding
type-wrapper class
(in
package
java.lang
). These classes are called
Boolean
,
Byte
,
Character
,
Double
,
Float
,
Integer
,
Long
and
Short
. These enable you to manipulate primitive-type values as ob-
jects. This is important because the data structures that we reuse or develop in
Chapters 16-21 manipulate and share
objects
—they cannot manipulate variables of prim-
itive types. However, they can manipulate objects of the type-wrapper classes, because ev-
ery class ultimately derives from
Object
.
Each of the numeric type-wrapper classes—
Byte
,
Short
,
Integer
,
Long
,
Float
and
Double
—extends class
Number
. Also, the type-wrapper classes are
final
classes, so you
cannot extend them. Primitive types do not have methods, so the methods related to a
primitive type are located in the corresponding type-wrapper class (e.g., method
parseInt
,
which converts a
String
to an
int
value, is located in class
Integer
).
Java provides boxing and unboxing conversions that automatically convert between prim-
itive-type values and type-wrapper objects. A
boxing conversion
converts a value of a
primitive type to an object of the corresponding type-wrapper class. An
unboxing conver-
sion
converts an object of a type-wrapper class to a value of the corresponding primitive
type. These conversions are performed automatically—called
autoboxing
and
auto-un-
boxing
. Consider the following statements:
Integer[] integerArray =
new
Integer[
5
];
// create integerArray
integerArray[
0
] =
10
;
// assign Integer 10 to integerArray[0]
int
value = integerArray[
0
];
// get int value of Integer
In this case, autoboxing occurs when assigning an
int
value (
10
) to
integerArray[0]
, be-
cause
integerArray
stores references to
Integer
objects, not
int
values. Auto-unboxing
occurs when assigning
integerArray[0]
to
int
variable
value
, because variable
value
stores an
int
value, not a reference to an
Integer
object. Boxing conversions also occur in
conditions, which can evaluate to primitive
boolean
values or
Boolean
objects. Many of
the examples in Chapters 16-21 use these conversions to store primitive values in and re-
trieve them from data structures.
Interface
Collection
contains
bulk operations
(i.e., operations performed on an
entire
collection) for operations such as
adding
,
clearing
and
comparing
objects (or elements) in a
collection. A
Collection
can also be converted to an array. In addition, interface
Collec-
tion
provides a method that returns an
Iterator
object, which allows a program to walk
through the collection and remove elements from it during the iteration. We discuss class
Iterator
in Section 16.6.1. Other methods of interface
Collection
enable a program to
determine a collection's
size
and whether a collection is
empty
.