简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM