Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
This method compares the invoking object withobj.It returns 0 if the values are equal. A
negative value is returned if the invoking object has a lower value. Otherwise, a positive
value is returned.
This interface is implemented by several of the classes already reviewed in this book.
Specifically, theByte,Character,Double,Float,Long,Short,String, andIntegerclasses
define acompareTo( )method. In addition, as the next chapter explains, objects that implement
this interface can be used in various collections.

The Appendable Interface


Objects of a class that implementsAppendablecan have a character or character sequences
appended to it.Appendabledefines these three methods:

Appendable append(charch) throws IOException
Appendable append(CharSequencechars) throws IOException
Appendable append(CharSequencechars, intbegin, intend) throws IOException

In the first form, the characterchis appended to the invoking object. In the second form, the
character sequencecharsis appended to the invoking object. The third form allows you to
indicate a portion (specified bybeginandend) of the sequence specified bychars. In all cases,
a reference to the invoking object is returned.

The Iterable Interface


Iterablemust be implemented by any class whose objects will be used by the for-each
version of theforloop. In other words, in order for an object to beused within a for-each
styleforloop, its class must implementIterable.Iterableis a genericinterface that has this
declaration:

interface Iterable<T>

Here,Tis the type of the object being iterated. It defines one method,iterator( ), which is
shown here:

Iterator<T> iterator( )

It returns an iterator to the elements contained in the invoking object.

NOTEOTE Iterators are described in detail in Chapter 17.


The Readable Interface


TheReadableinterface indicates that an object can be used as a source for characters.
It defines one method calledread( ), which is shown here:

int read(CharBufferbuf) throws IOException

434 Part II: The Java Library

Free download pdf