The
CharSequence Interface
The CharSequence interface defines methods that grant read-only access to a sequence of characters. These methods are shown in Table 17-23. This interface is implemented by String, StringBuffer, and StringBuilder, among others.
The
Comparable Interface
Objects of classes that
implement Comparable can be ordered.
In other words, classes that implement Comparable
contain objects that can be compared in some meaningful manner. Comparable is generic and is declared
like this:
interface Comparable<T>
Here, T represents the type of objects being compared.
Method : Description
char charAt(int idx) : Returns the character at
the index specified by idx.
default IntStream chars( ) : Returns a stream
(in the form of an IntStream) to the characters in the invoking object. (Added
by JDK 8.)
default IntStream codePoints( ) : Returns a
stream (in the form of an IntStream) to the code points in the invoking object.
(Added by JDK 8.)
int length( ) : Returns the number of
characters in the invoking sequence.
CharSequence subSequence(int startIdx, int
stopIdx) : Returns a subset of the invoking sequence beginning at startIdx and
ending at stopIdx–1.
String toString( ) : Returns the String
equivalent of the invoking sequence.
Table
17-23 The Methods Defined by CharSequence
The Comparable interface declares one method that is used to determine
what Java calls the natural ordering
of instances of a class. The signature of the method is shown here:
int compareTo(T obj)
This method compares the
invoking object with obj. 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, the Byte, Character, Double, Float, Long, Short, String, and Integer classes define a compareTo(
) method. So does Enum.
The
Appendable Interface
Objects of a class that
implements Appendable can have a
character or character sequences appended to it. Appendable defines these three methods:
Appendable append(char ch) throws IOException Appendable
append(CharSequence chars) throws
IOException
Appendable
append(CharSequence chars, int begin, int end) throws IOException
In the first form, the
character ch is appended to the
invoking object. In the second form, the character sequence chars is appended to the invoking
object. The third form allows you to indicate a portion (the characters running
from begin through end–1) of the sequence specified by chars. In all cases, a reference to the
invoking object is returned.
The
Iterable Interface
Iterable must be implemented by any class whose objects will be used by the
for-each version of the for loop. In other words, in order for
an object to be used within a for-each style for loop, its class must implement Iterable. Iterable is a
generic interface that has this declaration:
interface Iterable<T>
Here, T is the type of the object being iterated. It defines one abstract
method, iterator( ), which is shown
here:
Iterator<T> iterator( )
It returns an iterator to the
elements contained in the invoking object.
Beginning with JDK 8, Iterable also defines two default
methods. The first is called forEach( ):
default void
forEach(Consumer<? super T> action)
For each element being
iterated, forEach( ) executes the
code specified by action. (Consumer is a functional interface
added by JDK 8 and defined in java.util.function.
See Chapter 19.)
The second default method is spliterator( ), shown next: default
Spliterator<T> spliterator( )
It returns a Spliterator to the sequence being
iterated. (See Chapters 18 and 29 for details on spliterators.)
The
Readable Interface
The Readable interface indicates that an object can be used as a source
for characters. It defines one method called read( ), which is shown here:
int read(CharBuffer buf ) throws IOException
This method reads characters
into buf. It returns the number of
characters read, or –1 if an EOF is encountered.
The
AutoCloseable Interface
AutoCloseable provides support for the try-with-resources
statement, which implements what is
sometimes referred to as automatic resource
management (ARM). The try-with-resources
statement automates the process of releasing a resource (such as a stream) when
it is no longer needed. (See Chapter 13 for details.) Only objects of classes
that implement AutoCloseable can be
used with try-with-resources. The AutoCloseable interface defines only the close( ) method, which is shown here:
void close( ) throws
Exception
This method closes the
invoking object, releasing any resources that it may hold. It is automatically
called at the end of a try-with-resources
statement, thus eliminating the need to explicitly invoke close( ). AutoCloseable
is implemented by several classes, including all of the I/O classes that open a
stream that can be closed.
The
Thread.UncaughtExceptionHandler Interface
The static Thread.UncaughtExceptionHandler
interface is implemented by classes that want to handle uncaught exceptions. It
is implemented by ThreadGroup. It
declares only one method, which is shown here:
void uncaughtException(Thread
thrd, Throwable exc)
Here, thrd is a reference to the thread that generated the exception and exc is a reference to the exception.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.