Java Reference
In-Depth Information
Number
Each of
Float
,
Double
,
Integer
,
Long
,
Short
, and
Byte
provides the other
classes'
x
Value()
methods as well as its own
x
Value()
method. For example,
Float
provides
doubleValue()
,
intValue()
,
longValue()
,
shortValue()
, and
byteValue()
as well as
floatValue()
.
Allsixmethodsaremembersof
Number
,whichistheabstractsuperclassof
Float
,
Double
,
Integer
,
Long
,
Short
, and
Byte
—
Number
's
floatValue()
,
doubleValue()
,
intValue()
, and
longValue()
methods are abstract.
Num-
ber
is also the superclass of
java.math.BigDecimal
and
java.math.BigInteger
(discussed later in this chapter), and a pair of
Number
exists to simplify iterating over a collection of
Number
subclass objects.
For example, you can declare a variable of
java.util.List<Number>
type and
initialize it to an instance of
java.util.ArrayList<Number>
(or
Ar-
rayList<>
, for short). You can then store a mixture of
Number
subclass objects in
thecollection,anditerateoverthiscollectionbycallingasubclassmethodpolymorph-
ically.
Reference
Chapter2
introducedyoutogarbagecollection,whereyoulearnedthatthegarbagecol-
lectorremovesanobjectfromtheheapwhentherearenomorereferencestotheobject.
This statement isn't completely true, as you will shortly discover.
whereyoulearnedthatthegarbagecollectorcallsthismethodbeforeremovinganobject
from the heap. The
finalize()
method gives the object an opportunity to perform
cleanup.
Thissectioncontinuesfromwhere
Chapter2
leftoffbyintroducingyoutoJava'sRe-
ferenceAPI.Afteracquaintingyouwithsomebasicterminology,itintroducesyoutothe
API's
Reference
and
ReferenceQueue
classes,followedbytheAPI's
SoftRe-
ference
,
WeakReference
, and
PhantomReference
classes. These classes let
applications interact with the garbage collector in limited ways.
Note
Aswellasthissection,youwillfindBrianGoetz's“Javatheoryandpractice:
Plugging memory leaks with soft references” (
http://www.ibm.com/de-
veloperworks/java/library/j-jtp01246/index.html
) and “Java the-