Why fist call of f() compiles fine? The argument given (and Boxed) is Integer, but explicit type witness is String! It should fail at compile time - just like it fails in the second call.
class Test<T> {
<S> void f(S arg) {
}
}
// for My<T> both f() calls give same resuls...
public class My {
public static void main(String[] args) {
Test obj = new Test<>();
obj.<String>f(4); // compiles (and runs) fine!
//c.ERR
//parameterized method <String>f(String) of type Test<Object>
//is not applicable for the arguments (Integer)
new Test<>().<String>f(4);
But if I make Test not a generic class, compilation fails as expected:
class Test {
<S> void f(S arg) {
}
}
public class My {
public static void main(String[] args) {
Test obj = new Test();
// compile err
//parameterized method <String>f(String)
//of type Test is not applicable for the arguments (Integer)
obj.<String>f(4);
}
}
P.S. Eclipse compiler, SE7 mode