Java Reference
In-Depth Information
Integer(int
num
)
Integer(String
str
) throws NumberFormatException
Double(double
num
)
Double(String
str
) throws NumberFormatException
If
str
does not contain a valid numeric value, then a
NumberFormatException
is thrown.
All of the type wrappers override
toString( )
. It returns the human-readable form of the
value contained within the wrapper. This allows you to output the value by passing a type
wrapper object to
println( )
, for example, without having to convert it into its primitive
type.
The process of encapsulating a value within an object is called
boxing
. Prior to JDK 5,
all boxing took place manually, with the programmer explicitly constructing an instance of
a wrapper with the desired value. For example, this line manually boxes the value 100 into
an
Integer
:
In this example, a new
Integer
object with the value 100 is explicitly created and a refer-
ence to this object is assigned to
iOb
.
The process of extracting a value from a type wrapper is called
unboxing
. Again, prior
to JDK 5, all unboxing also took place manually, with the programmer explicitly calling a
method on the wrapper to obtain its value. For example, this manually unboxes the value
in
iOb
into an
int
.
Here,
intValue( )
returns the value encapsulated within
iOb
as an
int
.
The following program demonstrates the preceding concepts: