I faced with code, which compilation result was surprised for me.
public class Test3{
public static<K,V> Map<K,V> map(){return new HashMap<K,V>();}
}
class A{
static void f(Map<String,Integer> bcMap){}
public static void main(String[] args){
f(Test3.map()) //not valid
Map<String,Integer> m = Test3.map();//valid
}
}
Always I supposed that if I pass value to method it means that method argument assigns to passed value.
Is it wrong ratification?
Correction:
Your use of Test3.map()
doesn't provide type arguments and there is no way for the compiler to infer the type arguments. The JLS says the following when failing to infer type arguments
Any remaining type variable
T
that has not yet been inferred is then inferred to have typeObject
.
So the method invocation looks like
Map<Object, Object> object = Test3.map();
f(object); //not valid
which is confirmed by what the error message from the compiler says:
incompatible types:
java.util.Map<java.lang.Object,java.lang.Object>
cannot be converted tojava.util.Map<java.lang.String,java.lang.Integer>
the generic type arguments default to Object
.
You can fix it by specifying the type arguments
f(Test3.<String, Integer>map()); // 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