Which method is called? (Integer… a) vs. (int a, int b)

后端 未结 5 563
无人共我
无人共我 2021-02-05 06:10

I just found out about a very interesting Java trick:

void method1(Integer... a){
}

So you can give this method as many integers as you want.

5条回答
  •  一个人的身影
    2021-02-05 06:47

    The second method wins. According to the Java Language Specification (pdf),

    The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation.

    If an applicable method is found during this stage, that method wins; no further search is performed. In your case, it happens to be the second method, because the first one is a variable arity method that also requires boxing.

提交回复
热议问题