tags:

views:

138

answers:

8

I've used Java for some time and I keep hearing about interfaces such as Cloneable, Iterable and other X-ables.

I was wondering if there is a list somewhere of all of these and more importantly - which ones do you regularly use day-to-day?

For example, I've read that Cloneable is considered badly written and isn't widely used.

A: 

Runnable is the one I use most.

Donal Fellows
I usually extend Thread. Is there any difference besides not being able to extend any other class?
puddingfox
No, but that's a very big difference. OK, sometimes I'll use a `Runnable` without having it directly plugged into a `Thread`; that's actually quite useful (and it's better to call the method `run` than `doIt`). Doing that with a lot of `Thread` instances would be very expensive (they're not cheap objects, even before you start them running).
Donal Fellows
Using Runnable rather than Thread separates the what you want to execute from how you want to execute it. With thread - they are bound - you execute run() on a new thread. With Runnable, you have a whole host of options - execute on the EDT with SwingUtilities.invokeLater, execute in a thread pool with ExecutorService, execute to a schedule (using Timer), etc...
mdma
A: 

Comparable is one I use all the time. There is a list: http://java.sun.com/j2se/1.5.0/docs/api/ but it is HUGE. (You have to scroll about 2/3 of the way down the page to get to the interface tree.

puddingfox
+1  A: 

You're correct re. Cloneable and to partly answer your question, I would never use it. For more info read this interview with Joshua Bloch.

Brian Agnew
+2  A: 

Here's a list of all of the *able classes and interfaces in java. There's a only a few that are really widely used: I'd add Comparable and Runnable to your list.

$ jar tf $JAVA_HOME/jre/lib/rt.jar | grep [a-z]able\.class | grep -v ^com | grep -v ^sun | sort
java/awt/Adjustable.class
java/awt/Container$WakingRunnable.class
java/awt/datatransfer/Transferable.class
java/awt/Dialog$WakingRunnable.class
java/awt/ItemSelectable.class
java/awt/print/Pageable.class
java/awt/print/Printable.class
java/awt/ScrollPaneAdjustable.class
java/io/Closeable.class
java/io/Externalizable.class
java/io/Flushable.class
java/io/Serializable.class
java/lang/Appendable.class
java/lang/Cloneable.class
java/lang/Comparable.class
java/lang/Iterable.class
java/lang/ProcessEnvironment$Variable.class
java/lang/Readable.class
java/lang/reflect/TypeVariable.class
java/lang/Runnable.class
java/lang/Throwable.class
java/rmi/activation/Activatable.class
java/util/Collections$SelfComparable.class
java/util/concurrent/Callable.class
java/util/concurrent/Executors$PrivilegedCallable.class
java/util/Formattable.class
java/util/Hashtable.class
java/util/Observable.class
javax/accessibility/AccessibleStreamable.class
javax/lang/model/type/TypeVariable.class
javax/management/remote/JMXAddressable.class
javax/naming/Referenceable.class
javax/script/Compilable.class
javax/script/Invocable.class
javax/security/auth/Destroyable.class
javax/security/auth/Refreshable.class
javax/sql/rowset/Joinable.class
javax/swing/JSlider$1SmartHashtable.class
javax/swing/JTable$ThreadSafePrintable.class
javax/swing/plaf/basic/BasicFileChooserUI$FileTransferHandler$FileTransferable.class
javax/swing/plaf/basic/BasicTextUI$TextTransferHandler$TextTransferable.class
javax/swing/plaf/basic/BasicTransferable.class
javax/swing/RepaintManager$DisplayChangedRunnable.class
javax/swing/Scrollable.class
javax/swing/SwingWorker$DoSubmitAccumulativeRunnable.class
javax/swing/TablePrintable.class
javax/swing/text/DefaultStyledDocument$ChangeUpdateRunnable.class
javax/swing/TransferHandler$PropertyTransferable.class
javax/swing/undo/StateEditable.class
org/omg/CORBA/portable/Streamable.class
ataylor
unix ftw ! ! ! !
Claudiu
@ataylor Way to show all us one line responders up +1
puddingfox
I like Closeable and Flushable - it makes code that used to have to has to know what kind of stream it is dealing stream-neutral since JDK 1.5.
mdma
Some classes ending in Hashtable and Variable snuck in there. I don't think they quite fit with the rest. ;-)
Don Roby
...and throwable :) (not an interface :)
aioobe
Oops, yeah some classes that don't match the question slipped in there. What I wanted to show is you can learn a lot about the Java runtime by poking around the jars shipped with the JRE.
ataylor
+2  A: 

From the API docs:

AccessibleStreamable
AdapterActivatorOperations
Callable
Cloneable
Closeable
Comparable
Compilable
Destroyable
Externalizable
Flushable
Formattable
Invocable
ItemSelectable
Iterable
JMXAddressable
Joinable
Pageable
Printable
Readable
Referenceable
Refreshable
Runnable
Scrollable
Serializable
StateEditable
Streamable
Transferable
TypeVariable
TypeVariable
VM_TRUNCATABLE

I use Cloneable, Comparable, Iterable, Runnable and of course Throwable :-)

aioobe
Waaaait... Throwable is not an interface. I do too use it more often than probably necessary. Nice catch :)
Daniil
Oooo.. I missed the *interface* part... I'll update the list ;-)
aioobe
updated. This is the list of interfaces :-)
aioobe
A: 

There are lots of interfaces, just as there are lots of classes, enums, and exceptions. If you just look at interfaces in isolation you will not see the complete picture. Some interfaces are nouns made into adjectives (-able) others are not - and the division is as much about what's sensible in English than any technical distinction.

It's probably best to investigate in the area you are trying to solve rather than looking into what interfaces are available across the JRE - most won't make much sense until you have a specific problem scenario in mind, and look at them in context with their collaborators.

As you are starting out, begin with interfaces in the java.lang package, then java.io, java.util, and possibly java.util.concurrent, this will give you a good grounding, and then look into specific application areas.

Good luck!

mdma
A: 

There's a list of all the interfaces in the Java library's javadocs - follow the tree link then search for the "Interface Hierarchy" section.

Pete Kirkham
+2  A: 

The Interfaces you're most likely to implement are:
java.lang.Comparable
java.lang.Runnable
java.io.Serializable

Interfaces that you're most likely to call methods on but not implement yourself are:
java.lang.Appendable (StringBuffer / StringBuilder / Writers)
java.lang.CharSequence (String / StringBuffer / StringBuilder)
java.lang.Iterable (Collections, either explicitly or with for Blah blah : List<Blah>)
java.lang.Readable (Readers)
java.io.Closeable (Streams)
java.io.Flushable (Streams)
java.util.Collection (Collections)
java.util.Deque (Collections)
java.util.List (Collections)
java.util.Map (Collections)
java.util.Set (Collections)

Interfaces that are most likely to blow up in your face:
java.lang.Cloneable

Edit: Whoops, Throwable's not an interface.

Usually, it's better to write a Copy Constructor rather than use a clone() method.

R. Bemrose