Search code examples
javawildcardtype-inferencegeneric-type-argument

Confusion over Java generic method type inference


The sample method is given below:

static <T> void doSomething(List<? super T> list1, List<? extends T> list2) { }

I am wondering what type will be inferred in this situation and by what logic. Let's say I am calling this method like this:

doSomething(new ArrayList<Object>(), new ArrayList<String>());

Would T type evaluate as Object or String?


Solution

  • This call does not bind T to a specific class. Java does not need to know the exact T because of type erasure implementation of the generics. As long as the types that you pass are consistent with the declaration, the code should compile; in your case, lists of Object and String are consistent with the declaration.

    Let's expand your code a little so that we could force binding of T to a specific type. Perhaps the easiest way to do it is to pass Class<T>, like this:

    static <T> void doSomething(List<? super T> list1, List<? extends T> list2, Class<T> cl) {
        System.out.println(cl);
    }
    

    Now let us try calling doSomething with String.class and with Object.class:

    doSomething(new ArrayList<Object>(), new ArrayList<String>(), Object.class);
    doSomething(new ArrayList<Object>(), new ArrayList<String>(), String.class);
    

    Both calls successfully compile, producing the output

    class java.lang.Object
    class java.lang.String
    

    Demo on ideone.