Generics – rule variations in bounded wildcards in java-8?
I am in the tutorial of defining this static method according to generics in Java:
public static <T extends Comparable<T>> T min(T a) { ... }
And say
min(new GregorianCalendar());
Cannot compile because gregoriancalendar extends calendars and calendars to implement comparable < calendar >, it implies that gregoriancalendar implements comparable < calendar > and not comparable < gregoriancalendar > Therefore, in order to compile, the signature must be changed to:
public static <T extends Comparable<? super T>> T min(T a) { ... }
This is perfectly understandable The first version of this method does not effectively compile in java-5, but it compiles in java-8! (I tried 5 to 8)
Why is java-8 now allowed? Because it now makes it more chaotic What are the new "rules" behind that?
Solution
Type inference!
There is a lot of information about this in JLS § 18 Specifically, I will guide you to JLS § 18.2 (page 678), which states:
In your case, let s = Gregorian calendar and T = calendar This page explains (during zooming out) that if s is a subtype of T, s is considered type T (Gregorian calendar is regarded as calendar)