Java Reference
In-Depth Information
Although
NumberFormatException
describes anunchecked exception, andal-
thoughuncheckedexceptionsareoftennothandledbecausetheyrepresentcodingmis-
takes,
NumberFormatException
doesnotfitthispatterninthisexample.Theex-
ceptiondoesnotarisefromacodingmistake;itarisesfromsomeonepassinganillegal
numeric argument to the application, which cannot be avoided through proper coding.
Perhaps
NumberFormatException
should have been implemented as a checked
exception type.
Integer, Long, Short, and Byte
Integer
,
Long
,
Short
,and
Byte
store32-bit,64-bit,16-bit,and8-bitintegerval-
ues in
Integer
,
Long
,
Short
, and
Byte
objects, respectively.
Eachclassdeclares
MAX_VALUE
and
MIN_VALUE
constantsthatidentifythemax-
imum and minimum values that can be represented by its associated primitive type.
These classes also declare the following constructors for initializing their objects:
•
Integer(int value)
initializes the
Integer
object to
value
.
•
Integer(String s)
converts
s
'stexttoa32-bitinteger valueandstores
this value in the
Integer
object.
•
Long(long value)
initializes the
Long
object to
value
.
•
Long(String s)
converts
s
's text to a 64-bit integer value and stores this
value in the
Long
object.
•
Short(short value)
initializes the
Short
object to
value
.
•
Short(String s)
converts
s
'stexttoa16-bitintegervalueandstoresthis
value in the
Short
object.
•
Byte(byte value)
initializes the
Byte
object to
value
.
•
Byte(String s)
converts
s
's text to an 8-bit integer value and stores this
value in the
Byte
object.
Integer
's constructors are complemented by
int intValue()
,
Long
's con-
structorsarecomplementedby
long longValue()
,
Short
'sconstructorsarecom-
plemented by
short shortValue()
, and
Byte
's constructors are complemented
by
byte byteValue()
. These methods return wrapped integers.
These classes declare various useful integer-oriented methods. For example,
In-
teger
declares the following utility methods for converting a 32-bit integer to a
java.lang.String
instance according to a specific representation (binary, hexa-
decimal, octal, and decimal):