Java Reference
In-Depth Information
Now consider this invocation of m :
m(new Object(), 29);
Again both forms are potentially applicable. The type of the generic
method is inferred to be <Object,Integer> after applying boxing conver-
sion to the argument 29. The non-generic form fails to match in the first
argument because an Object is not compatible with a String , so it is not
applicable. The generic method matches in the first argument exactly,
and matches in the second argument after a boxing conversion (phase
2). Consequently, the generic method is the only applicable method.
In contrast, this invocation of m is ambiguous:
m("hello", Integer.valueOf(29));
Both forms are potentially applicable, and again only phase 1 will come
into play. The inferred type of the generic method call is <String,Integer>
. The non-generic form matches exactly in the first argument type and
matches in the second argument because an Integer is an Object . The
generic form matches exactly on both argument types. So there are two
applicable methods. The next step is to determine which is the most
specific method. First, we see if the non-generic method is more specific
than the generic method. This is done as follows:
1. Since our second method is generic, type inference is performed,
but this time with the initial constraint that String is convertible
to S and that Object is convertible to T . This infers the types
<String,Object> for the generic method. Note that the bound on T
is not considered at this point.
2. A check is made to see if each parameter of the first method is a
subtype of the (inferred) type of the corresponding parameter in
the second method. In this case the parameter types of the first
 
Search WWH ::




Custom Search