Confusion over Java generic method type inference

J R

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?

Sergey Kalinichenko

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Confusion over Java generic method type inference

From Dev

Generic factory method and type inference

From Dev

Java 8: generic type inference fails on method reference?

From Dev

Type inference in generic method & generic class

From Dev

Generic type inference limits in Java

From Dev

Generic type inference not working with method chaining?

From Dev

Generic type parameters inference in method chaining

From Dev

Java type inference of generic exception type

From Dev

Java 8: Generic type inference improvements

From Dev

Java 8: Generic type inference improvements

From Dev

Generic method type inference when the target type has a wildcard

From Dev

Generic method type inference when the target type has a wildcard

From Dev

Generic type inference explanation

From Dev

Generic methods type inference

From Dev

FSharp: Type inference with generic

From Dev

Swift Generic Type Inference

From Dev

Swift 3 type inference confusion

From Dev

Confusion with type inference and functional dependencies

From Dev

Java 8 type inference static method call

From Dev

Confusion over Java HashMap createEntry method

From Dev

Why does the Java 8 generic type inference pick this overload?

From Dev

Java 8 Type Inference - How reduction is done for generic constructors?

From Dev

Java7 Type inference for generic instance creation?

From Dev

Java generic method type argument

From Dev

Java generic method type argument

From Dev

java generic method return type

From Dev

Generic type in interface method Java

From Dev

Type inference of generic function parameter

From Dev

Generic Type Inference with Class Argument

Related Related

HotTag

Archive