Search code examples
javagenericsvariable-assignmentargument-passinggeneric-method

Different behaviour for generic method return value to method and to assignment


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?


Solution

  • 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 type Object.

    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 to java.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