Java: overloaded method resolution and varargs — confusing example

左心房为你撑大大i 提交于 2019-11-30 08:37:09
Andy Thomas

In section 8.4.1:

If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[].

Since you're explicitly providing an array, this allows the second two calls to match the variable arity method in the first phase, without consideration of variable arity.

Vararg methods can be called with multiple parameters (a, b, c) or as an array ({a, b, c}). Because you are passing an array that matches the type of the varargs it takes precedence.

Reference: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!