THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1
characters (including leading or trailing whitespace), the value is out of range
for this type, or radix is out of range.

public staticTypevalueOf(String str, int radix)

Returns a wrapper object of class Type with the value obtained by decoding
str using the specified radix. Note that there are no equivalent constructors
with this form. This is equivalent to the two-step process of parsing the string
and using that value to construct a new wrapper object. For example,
Integer.valueOf("1010",2) is equivalent to the more verbose
expression newInteger(Integer.parseInt("1010",2)).

public staticTypedecode(String str)

Returns a wrapper object of class Type with the value obtained by decoding
str. A leading character indicates a negative value. The base of the number
is encoded in str: Hexadecimal numbers start with #, 0x, or 0X, while octal
numbers are indicated by a leading 0 ; otherwise, the number is presumed to
be decimal. This is in contrast to the parseType and valueOf methods,
to which you pass the radix as an argument. For example,
Long.decode("0xABCD") returns a Long with the value 43981, while
Long.decode("ABCD") tHRows a NumberFormatException.
Conversely, Long.parseLong("0xABCD", 16) throws a
NumberFormatException, while Long.parseLong("ABCD", 16)
returns a Long with the value 43981. (The character # is used in many
external data formats for hexadecimal numbers, so it is convenient that
decode understands it.)

For each class, the string-based constructor, parseType(String str) method, and valueOf methods
work like parseType(String str,intradix) , with a radix of 10 if none is specified


In addition, the Integer and Long classes each have the following methods, where type is either int or
long, respectively:


public static StringtoString(typeval, int radix)

Returns a string representation of the given value in the given radix. If radix
is out of range then a radix of 10 is used.

public static StringtoBinaryString(typeval)

Returns a string representation of the two's complement bit pattern of the
given value. For positive values this is the same as toString(value,2).
For negative values the sign of the value is encoded in the bit pattern, not as a
leading minus character. For example,
Integer.toBinaryString(-10) returns
"11111111111111111111111111110110". These string
representations for negative values cannot be used with corresponding
parseType methods because the magnitude represented by the string will
always be greater than MAX_VALUE for the given type.

public static StringtoOctalString(typeval)

Returns a string representation of the given value in an unsigned base 8
Free download pdf