简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM