genericsjava-8typecheckingbounded-wildcardbounded-types

rule changing in bounded wildcards in java-8?


I was following a tutorial about generics in Java defining this static method:

public static <T extends Comparable<T>> T min(T a) { ... }

and saying that

min(new GregorianCalendar());

couldn't compile because GregorianCalendar extends Calendar and Calendar implements Comparable<Calendar> so it implied that GregorianCalendar implements Comparable<Calendar> and NOT Comparable<GregorianCalendar>. So in order to compile the signature must be changed into:

public static <T extends Comparable<? super T>> T min(T a) { ... }

which is totally understandable. The 1st version of the method effectively doesn't compile in java-5 but it compiles in java-8! (i tried 5 through 8)

Why java-8 now allows that? (because it makes it more confusing now). What's the new "rule" behind that?


Solution

  • Type inference!

    There is a significant amount of information regarding this in JLS §18. Specifically, I'll direct you to JLS §18.2 (page 678) which states:

    enter image description here

    In your case, let S = GregorianCalendar and T = Calendar. This page states (during the reduction process) if S is a sub-type of T, then S is considered to be of type T (GregorianCalendar is treated as Calendar).