Java method dispatch with null argument

前端 未结 4 1390
孤独总比滥情好
孤独总比滥情好 2020-11-27 08:38

Why does it (apparently) make a difference whether I pass null as an argument directly, or pass an Object that I assigned the value

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 08:59

    Because the second commented out invocation with null is ambiguous to the compiler. The literal null could be a string or an object. Whereas the assigned value has a definite type. You need to cast the null, e.g. test.foo((String)null) to remove the ambiguity.

提交回复
热议问题