Java varargs - why are these unacceptable syntax?

122 Views Asked by At

I had this practice test question for my OCAJP8 exam preparation. Can someone please explain why the 'Wrong' ones are wrong? Thanks.

Which of the following compile?

Response

Wrong   
public void moreD(String... values, int... nums) {}

Wrong   
public void moreF(String... values, int[] nums) {}

Correct 
public void moreB(String values, int... nums) {}

Correct 
public void moreG(String[] values, int[] nums) {}

Correct 
public void moreA(int... nums) {}

Wrong   
public void moreC(int... nums, String values) {}
1

There are 1 best solutions below

0
Trevor Freeman On BEST ANSWER

To quote from https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

Varargs can be used only in the final argument position.