I just stumbled upon the following overloads of sort in java.util.Arrays:
public static void sort(Object[] a)
public static <T> void sort(T[] a, Comparator<? super T> c)
Why is the first overload not generic, but the second one is? Why does the first overload take an Object[] instead of a Comparable[]? The documentation even states:
All elements in the array must implement the
Comparableinterface.
So what's the point of not verifying that constraint with Java's static type system?
Comparable[]before generics, right?TreeSethas the same "bizzareness": you can actually created aTreeSetwith a class not implementingComparable, but then at runtime you'll getClassCastException. Arrays are another problem though, sinceA[]is not the same asB[]at runtime, unlike genericized collections.TreeSettakes elements that do not implementComparableif you provide aComparator