The method is ambiguous for the type Error

好久不见. 提交于 2019-12-05 06:43:48

There are 3 stages to method overloading resolution. The first stage doesn't do auto-boxing/unboxing, which means methods that require boxing/unboxing of the passed parameters in order to match one of the overloaded versions of add will only be considered if no match was found that doesn't require boxing/unboxing. That's why 3 of your calls, which have a single exact match, work. Regarding add(b,c);, see below why it's ambiguous.

   add(a,c); // exact match to add(int a, long b)
   add(a,d); // exact match to add(int a, Long b)
   add(b,c); // there is no exact match, so at least one of the passed parameters must
             // be boxed or unboxed. However, by unboxing b to int or boxing 
             // c to Long, each of the three add methods can match, and the 
             // compiler doesn't know which one to prefer
   add(b,d); // exact match to add(Integer a, Long b)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!