public class SortedList<E extends Comparable> {
public void insert(E item);
...
}
is legitimate in Java 5 (because of compatibility with legacy code)
but nonetheless is not fully in the spirit of the new generics. The
reason is that the
Comparable
interface now takes a type parameter:
public interface Comparable<T> {
public int compareTo(T o);
}
so that compareTo only has to compare this with
another T and not with an arbitrary Object as
before. Thus the Integer class, for example, is now defined
by
public final class Integer ... implements Comparable<Integer> {
...
public int compareTo(Integer anotherInteger) {
...
}
}
Similar definitions are used for Float, String etc.
As a consequence, the use of the raw Comparable interface in
public class SortedList<E extends Comparable> {
...
}
is inadequately typed from the new perspective.
A simple modification to the SortedList class definition, to make it compliant with Java 5 generics, would be
public class SortedList<E extends Comparable<E>> {
public void insert(E item);
...
}
which requires that E must implement the
Comparable<E> interface. The following will then all be
legitimate:SortedList<Integer> ilist = new SortedList<Integer>(); SortedList<Float> flist = new SortedList<Float>(); SortedList<String> slist = new SortedList<String>();