THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

For the numeric types, comparison follows the normal arithmetic rules; however, the Float and Double
classes have specific ordering properties that differ from float and doublesee page 191. For Boolean,
the value true is considered greater than falsein contrast to boolean for which the relational operators <
and > are not defined. For Character, the comparison is strictly numerical, with no consideration of
alphabetic, or locale specific ordering.


Each class provides a method to extract the wrapped value:


publictypetypeValue()

Returns the primitive value corresponding to the current wrapper object. For
example, Integer.valueOf(6).intValue() returns 6.

And each class overrides the following members of Object:


public StringtoString()

Provides a string representation of the wrapper object's value. For the
numeric classes base 10 is always used.

public booleanequals(Object obj)

Returns true if the two objects are the same type and wrap the same value.
For example, for two Integer objects x and y, x.equals(y) is true if
and only if, x.intValue()==y.intValue(). If obj is not of the
same type as the current object, or is null, false is returned.

public inthashCode()

Returns a value-based hash code for use in hashtables.

The following additional methods convert primitive values to and from strings:


public statictypeparseType(String str)

Converts the string str to a value of the specified primitive type. For
example, Integer.parseInt("10") returns the value 10. This is
equivalent to invoking the string-converting constructor, and extracting the
resulting value with typeValue(), but without constructing an object.
Character does not have this method just as it does not have the
string-converting constructor.

public static StringtoString(typeval)

Returns a string representation of the given primitive value of type type.
For example, Double.toString(0.3e2) returns the string "30.0".

All wrapper classes have these methods so we do not list them in each class's description. The wrapper class's
system property fetch-and-decode methods, described in "System Properties" on page 663, are not discussed
in this chapter.

Free download pdf