I've created a generic method with two type paremeter as follow inside a non-generic class:
static <T, V extends T> boolean testMethod(T t, V[] v) {
return true;
}
The second type parameter defined as V extends T.
Now consider calling of this method by following arguments:
testMethod("text", new Integer[]{1, 2});
When I try to call this method with the above parameters I expected to got an error of type mismatch, because type parameter T substituted by String and V substituted by Integer and as you see V is bounded by T which means it must be of type String or its derived classes but Integer is non of them. But the code segment compile and run successfully without any problem.
Can somebody explain what is wrong about my impression of this concept?
In this method call, the values of the T and V type parameters are not provided explicitly, so the compiler infers them. It will always try to infer valid substitutions before returning an error, even if the results are not always intuitive. In this example, the value “text” is a String, yes, but it is also an Object. Since the inference T = String, V = Integer is invalid because Integer is not a subtype of String, the compiler uses T = Object, V = Integer, which is valid.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With