Generic ype inference with lower bounded types

Sam Munkes sam.munkes at
Sat Jan 3 00:07:32 UTC 2015

Hi Guys,

A colleague of mine recently posted the following question to

Does anyone on the list have insight into why the compiler does not infer
lower bounded types?

static class Test {
    static <T> T pick(T one, T two) {
        return two;

    static void testUpperBound() {
        List<? extends Integer> extendsInteger = new ArrayList<>();

        // List<? extends Integer> is treated as a subclass of List<?
extends Number>
        List<? extends Number> extendsNumber = extendsInteger;

        // List<? extends Number> is inferred as the common superclass
        extendsNumber = pick(extendsInteger, extendsNumber);

    static void testLowerBound() {
        List<? super Number> superNumber = new ArrayList<>();

        // List<? super Number> is treated as a subclass of List<? super
        List<? super Integer> superInteger = superNumber;

        // The inferred common type should be List<? super Integer>,
        // but instead we get a compile error:
        *superInteger = pick(superNumber, superInteger);*

        // It only compiles with an explicit type argument:
        superInteger = Test.<List<? super Integer>>pick(superNumber,


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the compiler-dev mailing list