SCJP question: Java method overloading with var-args. What is the rationale?

You are right that widening comes before boxing, which in turn comes before var-args But you seem to be treating the first method as callMethod(Integer i) not callMethod(Integer... i) Since both methods use var-args, there's a priority tie That is, neither one meets the criteria for boxing alone, but both meet the criteria for var-args Remember that it's illegal to widen, then box (although I did some research before posting this answer and found that it IS legal to box, then widen). Similarly, you won't get box, then var-args behavior; the compiler skips right to the var-args step and sees two methods that take var-args EDIT: I should clarify that you WILL get box-then-var-args behavior if there's no ambiguity. In other words, if there was only one callMethod() and it took Integer... I you would get Wrapper.

You are right that widening comes before boxing, which in turn comes before var-args. But you seem to be treating the first method as callMethod(Integer i), not callMethod(Integer... i). Since both methods use var-args, there's a priority tie.

That is, neither one meets the criteria for boxing alone, but both meet the criteria for var-args. Remember that it's illegal to widen, then box (although I did some research before posting this answer and found that it IS legal to box, then widen). Similarly, you won't get box, then var-args behavior; the compiler skips right to the var-args step and sees two methods that take var-args.

EDIT: I should clarify that you WILL get box-then-var-args behavior if there's no ambiguity. In other words, if there was only one callMethod(), and it took Integer... i, you would get "Wrapper.

I kinda thought that it tried the three approaches and the first one to be "invocable" would be invoked. Although the method takes (Integer... i), I sort of expected that boxing into an Integer would make that method "invocable". But, as you say, it seems that the subsequent "var-arg-ization" is also taken into account.

Cheers! – Markos Fragkakis Feb 23 '10 at 21:59.

OK, I can see that either of the two methods will work (if the other is commented out), but I also know that there is hierarchy on what happens if a primitive is not exactly matched by the type of the input of a method. The first thing that is tried is to widen the primitive. The second thing is to box the primitive.

So if there was a method taking an Integer, that would be the one invoked. The third priority is the var-args. Based on the above priority, I would expect that the second priotity be the case.

I would expect the int to be wrapped into an Integer and the (Integer...) would be invoked.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send