Java Reference
In-Depth Information
Converting between Primitive Type Values and Strings
Each class provides a static
toString()
method to convert a value of the corresponding primitive type to
a
String
object, as you saw in the last chapter. There is also a non-static
toString()
method in each class
that returns a
String
representation of a class object.
Conversely, there are methods to convert from a
String
object to a primitive type. For example, the
static
parseInt()
member in the
Integer
class accepts a
String
representation of an integer as an argu-
ment and returns the equivalent value as type
int
. An alternative version of this method accepts a second
argument of type
int
that specifies the radix to be used when interpreting the string. This enables you to
parse strings that are hexadecimal or octal values, for example. If the
String
object cannot be parsed for
any reason, if it contains invalid characters, for example, the method throws an exception of type
Num-
berFormatException
. All the standard classes encapsulating numerical primitive types and the
Boolean
class define static methods to parse strings and return a value of the corresponding primitive type. You have
the methods
parseShort()
,
parseByte()
,
parseInt()
, and
parseLong()
in the classes for integer types,
parseFloat()
and
parseDouble()
for floating-point classes, and
parseBoolean()
for
Boolean
.
The classes for primitive numerical types and the
Boolean
class defines a static method
valueOf()
that
converts a string to an object of the class type containing the value represented by the string. If the string
does not represent a valid value,
NumberFormatException
is thrown. The method for the
Boolean
class
returns a
Boolean
object with the value
true
if the string is equal to
true
ignoring case. Any other string
results in
false
being returned.
WARNING
At the time of writing, the methods, such as
parseInt()
,
for converting strings
to numerical values throw an exception of type
NumberFormatException
if the string to be
parsed contains underline characters, even though underlines are legal in numerical literals.
This is regarded as a bug so it may well be fixed by the time you are reading this. If not, you
can always write a method to remove underlines from any string that is to be converted to a
numerical value.
Converting Objects to Values
Each class encapsulating a primitive data value also defines a
xxxValue()
method (where xxx is the cor-
responding primitive type name) that returns the value that is encapsulated by an object as a value of the
corresponding primitive type. For example, if you have created an object
number
of type
Double
that encap-
sulates the value 1.14159, then the expression
number.doubleValue()
results in the value 1.14159 as type
double
.
Primitive Class Constants
The classes that wrap numerical primitive types each contain the
static final
constants
MAX_VALUE
and
MIN_VALUE
that define the maximum and minimum values that can be represented. The floating-point
classes also define the constants
POSITIVE_INFINITY
,
NEGATIVE_INFINITY
, and
NaN
(it stands for
N
ot
a
N
umber, as it is the result of 0/0), so you can use these in comparisons to test whether such values have
arisen during calculations. Alternatively, you can test floating-point values with the static methods
isIn-