2

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?

J R
  • 223
  • 3
  • 12

1 Answers1

6

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Another interesting example is with more class hierarchy `doSomething(new ArrayList(), new ArrayList(), Number.class);`. This shows that `T` does not actually have to match the type of either list. It simply must be "between" (inclusive) them. – Brett Okken Jun 22 '14 at 12:31