-3
public void test(int... integers, String str) { // error
   ...  
}

Error:

The variable argument type int of the method test must be the last parameter.

public void test(String str, int... integers) {}

It works well. Is there any reason for this?

Dimitar
  • 4,402
  • 4
  • 31
  • 47
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103

1 Answers1

6

Well, consider this method signature:

public void test(int... integers, float val, float val2)

Now, when you invoke this method:

test(2, 3, 4, 5, 6, 7);

How will compiler decide when to stop adding arguments to int... type parameter? Remember, arguments are evaluated from left-to-right in Java. That is why var-args should be at the end. So, that compiler can first assign the fixed parameters, and then rest of the arguments, go to the var-args.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 2
    Why could the compiler not see that 6 and 7 must be the float arguments and hence 2,3,4,5 the integers? Note, parameters would still be evaluated from left to right at runtime. As long as there is only one vararg, it could be anywhere in the parameter list. Everything else hints at lazy compiler writers. – Ingo Aug 16 '13 at 11:39