Community
Participate
Working Groups
Sample code: class Infer { private static <T extends Comparable<? super T>> T max(T... elems) { T max=null; for (T elem : elems) if (max == null || max.compareTo(elem) < 0) max=elem; return max; } public static void main(String[] args) { System.out.println(max(1, 2.0, new BigDecimal(Math.PI))); } } This code compiles fine in Eclipse 3.1 and throws ClassCastException in runtime. The whole point of generics is to prevent "unexpected" ClassCastExceptions such as this (when there are no warnings or errors) and it is true for this sample as well: this should not compile. The type inferred for <T> in max is Number but Number is *not* Comparable<Number>. javac says: src/jezuch/utils/starmapper3/test/Infer.java:18: <T>max(T...) in jezuch.utils.starmapper3.test.Infer cannot be applied to (int,double,java.math.BigDecimal); inferred type argument(s) java.lang.Number&java.lang.Comparable<? extends java.lang.Number&java.lang.Comparable<?>> do not conform to bounds of type variable(s) T System.out.println(max(1, 2.0, new BigDecimal(Math.PI)));
Added GenericTypeTest#test798 *** This bug has been marked as a duplicate of 103485 ***