Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

The CharSequence Interface


TheCharSequenceinterface defines methods that grant read-only access to a sequence of
characters. These methods are shown in Table 16-22. This interface is implemented byString,
StringBuffer, andStringBuilder. It is also implemented byCharBuffer, which is in the
java.niopackage (described later in this book).

The Comparable Interface


Objects of classes that implementComparablecan be ordered. In other words, classes that
implementComparablecontain objects that can be compared in some meaningful manner.
Comparable is generic and is declared like this:

interface Comparable<T>

Here,Trepresents the type of objects being compared.
TheComparableinterface declares one method that is used to determine what Java
calls thenatural orderingof instances of a class. The signature of the method is shown here:

int compareTo(Tobj)

Chapter 16: Exploring java.lang 433


Method Description
final String name( ) Returns the unaltered name of the invoking constant.
final int ordinal( ) Returns a value that indicates an enumeration
constant’s position in the list of constants.
String toString( ) Returns the name of the invoking constant. This
name may differ from the one used in the
enumeration’s declaration.
static <T extends Enum<T>> T
valueOf(Class<T>e-type, Stringname)

Returns the constant associated withnamein the
enumeration type specified bye-type.

TABLE 16-21 The Methods Defined byEnum(continued)

Method Description
char charAt(intidx) Returns the character at the index specified byidx.
int length( ) Returns the number of characters in the invoking
sequence.
CharSequence
subSequence(intstartIdx, intstopIdx)

Returns a subset of the invoking sequence beginning at
startIdxand ending atstopIdx–1.
String toString( ) Returns theStringequivalent of the invoking sequence.

TABLE 16-22 The Methods Defined byCharSequence
Free download pdf