此问题已经在这里有了答案:
鉴于void foo(Boolean... bars)
函数,bars
的行为与显然在以下函数调用中应有的行为完全不同:
bars = Boolean[0]
bars = null
bars = Boolean[2] { null, null }
为什么调用foo(null)会产生bars = null
而不是Boolean[1] { null }
?这是在Java 8上复制的。
这是由于determining method signature的三相方法:
[foo(null)
在阶段1中匹配,因为接受Boolean[]
的方法只允许使用一个空参数,因为可以将null
强制转换为Boolean[]
(或者实际上是任何引用类型)。
foo(null, null)
在阶段3中被匹配,因为那是可变Arity方法被匹配的时候。假设您没有foo
的2-arg重载,则在此之前无法对其进行匹配,因为匹配方法将需要两个参数。