Why does autoboxing make some calls ambiguous in Java?

前端 未结 6 1773
眼角桃花
眼角桃花 2020-11-27 15:09

I noticed today that auto-boxing can sometimes cause ambiguity in method overload resolution. The simplest example appears to be this:

public class Test {
           


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 16:07

    So why did the overload resolution fail? Why didn't the compiler auto-box the first argument, and accept the second argument normally? Why did I have to request auto-boxing explicitly?

    It didn't accept the second argument normally. Remember that "boolean" can be boxed to an Object too. You could have explicitly cast the boolean argument to Object as well and it would have worked.

提交回复
热议问题