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

后端 未结 5 546
无人共我
无人共我 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:42

    I'm half guessing, but like thinksteep said, Integer... is actually treated as an Integer array, which implies that your method call would have to do two coercions to make it match the first method (boxing the ints to Integers, and treating your argument list as an array rather than simply two different arguments). Whereas, no coercions are required to make it match the second method.

    OK, I can see several others have already quoted the JLS with more specificity than I have provided, while I was typing this out.

提交回复
热议问题