Reputation: 135
In oracle's official java document, Type Inference chapter, there's an example like this:
static <T> T pick(T a1, T a2) { return a2; }
Serializable s = pick("d", new ArrayList<String>());
In this case the type parameters are T but was passed two different type, shouldn't a1's type be the same as a2?
Upvotes: 3
Views: 55
Reputation: 8597
The inference algorithm determines the types of the arguments and, if available, the type that the result is being assigned, or returned. Finally, the inference algorithm tries to find the most specific type that works with all of the arguments.
And the most specific type for your example is Serializable
, which is ancestor to both String
and ArrayList
.
Upvotes: 9