The problem is that Integer
is specifically Comparable<Integer>
, Double
is specifically Comparable<Double>
, and so on, so to use their compareTo
, you have to pass in a reference of the appropriate type, which means that you have to have an object of the appropriate type. Note that the cast in (Double) new Integer(0)
will never succeed, because you can't have a reference of one type that points to an object of an incompatible type. (And actually it's a compile error, because the compiler can tell in this case that the cast will never succeed.)
I think your doubleValue
approach is probably the best one.