The subject line is the best I could do to describe something that caught me out in the first series of self-tests, question 3 in the
SCJP Sierra/Bates book.
Given:
So you have the same method called with two different method signatures.
The
test question is:
Which, inserted independently at line 6, will compile? (Choose all that apply.)
A. static void doStuff(int... doArgs) { }
B. static void doStuff(int[] doArgs) { }
C. static void doStuff(int doArgs...) { }
D. static void doStuff(int... doArgs, int y) { }
E. static void doStuff(int x, int... doArgs) { }
I answered A as the only correct answer and was surprised to find that E also is correct! The reason I didn't pick E was in the first case, doStuff(1), would not compile because it would be missing the varargs argument (there is only one argument, 1, after all). Surprisingly, though, not only does the code compile but it runs.
Can someone enlighten me why doStuff(1) (w/one argument) will compile and run with E which requires two arguments?