Consider these sorting functions in java:
public static <T extends Comparable<? super T>> void sort(T[] a){
}
vs
public static void sort(Comparable[] a){
}
Is there any difference between how you can use these methods? Can for example both the method take an object whose superclass implements a subclass of comparable?
I noticed that for the second method the eclipse editor complains about the function definition with this message:
“Comparable is a raw type. References to generic type Comparable should be parameterized.”
The first one ensures that all the elements of the array are comparable with each other. The second one only ensures that all the elements implement Comparable.
You may pass an array containing Integer, String and Date instances to the second method, but you can’t with the first one. The first one is much type-safer.