First, ArrayList
needs not be Comparable
. The things inside the array list need to be Comparable
. This is the signature of sort
:
public static <T extends Comparable<? super T>> void sort(List<T> list) {
T
is constrained, not the List
.
However, using raw types changes everything.
Because you wrote ArrayList
instead of ArrayList<String>
, you are using the raw type of ArrayList
. This basically erases all generic type parameters and makes sort
not check for any constraints. It is as if the signature became:
public static void sort(List list) {
That's why you were able to call it. Had you put non-Comparable
things into the array list, it would have crashed at runtime:
ArrayList al= new ArrayList();
al.add(new Object());
al.add(new Object());
Collections.sort(al); // ClassCastException