在Java语言规范(JLS)中以非常正式的方式定义最具体的方法。在尝试尽可能地删除正式公式时,我已经提取了适用的主要项目。
总结适用于您的问题的主要项目有:
> JLS 15.12.2:您的用例属于第3阶段:
The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.
>然后JLS 15.12.2.4基本上确定两种方法都适用,因为10可以转换为整数…或int …到目前为止这么好。该段落得出结论:
The most specific method (§15.12.2.5) is chosen among the applicable variable-arity methods.
这使我们到了JLS 15.12.2.5.这一段给出了一种比其他方法m(b …)更具体的方法m(a …)的条件。在你的用例中有一个参数,没有泛型,它归结为:
m(a...) is more specific than m(b...) iif a <: b where means is a subtype of.>
它发生在int不是Integer的子类型,Integer不是int的子类型。
为了使用JLS语言,这两种调用方法因此是最大的特定的(没有方法比其他方法更具体)。在这种情况下,同一段落得出结论:
If all the maximally specific methods have override-equivalent (§8.4.2) signatures […] => not your case as no generics are involved and Integer and int are different parameters
Otherwise, we say that the method invocation is ambiguous, and a compile-time error occurs.
注意
如果你以长整型替换Integer …例如,你将具有int
类似地,如果您用Number …替换int …,则调用(Integer …)方法将是最具体的。