4
method(1);  // This works -

void method(int... x) { }
void method(int x) { }  // - this method is called

If I add a varargs parameter to the second method, I get a "reference to method is ambiguous" compilation error:

method(1);  // This now fails

void method(int... x) { }
void method(int x, String... y) { }  // adding String... y causes a problem.

As the String... y argument(s) can be left "blank", why doesn't Java still pick that method? Thanks, and apologies if there is a closely matching explanation on SO; I did look for one.

user2911290
  • 1,396
  • 1
  • 16
  • 26
  • 1
    Could you share the exact compiler error you're getting? – Alexis Leclerc Apr 09 '14 at 12:30
  • @Alexis Leclerc ..\src\pkgs\main\Main.java:1278: error: reference to method is ambiguous, both method method(int...) in Main and method method(int,String...) in Main match method(1); – user2911290 Apr 09 '14 at 12:36
  • 1
    I copy pasted your methods and it compiled just fine – Gladhus Apr 09 '14 at 12:37
  • @Gladhus Cannot be. Did you copy the second one? – Maroun Apr 09 '14 at 12:39
  • Yup, If I comment out the first method, it calls the second one, and if I leave both of them uncommented, it calls the first one... Weird weird weird – Gladhus Apr 09 '14 at 12:40
  • On my side compiles fine too, please add more details about compiler and compiler settings – DayaMoon Apr 09 '14 at 12:40
  • [Guys.. This **DOESN'T** compile...](https://ideone.com/UyjYuF) – Maroun Apr 09 '14 at 12:42
  • Hey, don't blame me, I swear it does. I do understand why it **shouldn't** though – Gladhus Apr 09 '14 at 12:43
  • @Gladhus I'm not blaming you, just make sure you're running the case that *should* cause the compilation error. – Maroun Apr 09 '14 at 12:43
  • It compiles for me too, but I'm under Java 6 with Eclipse, if that makes any difference... – Alexis Leclerc Apr 09 '14 at 12:44
  • @DayaMoon, I would be very happy too. I'm a relative Java noob. Please can you tell me how to get this info? Some basic info for you: if I type javac -version at the Windows command prompt, I see 1.7.0_40. Also, I am using Notepad to write my programs - no IDE. – user2911290 Apr 09 '14 at 12:44
  • 1
    Only one explanation here... My java compiler **reads my mind** – Gladhus Apr 09 '14 at 12:45
  • @Gladhus: There has to be something... from IDE it compiles/runs, but from shell it does not compile. – DayaMoon Apr 09 '14 at 13:01
  • It seems there is a difference between IDE and JDK compiler... See http://stackoverflow.com/questions/3061654/what-is-the-difference-between-javac-and-the-eclipse-compiler and http://stackoverflow.com/questions/2858799/generics-compiles-and-runs-in-eclipse-but-doesnt-compile-in-javac – DayaMoon Apr 09 '14 at 14:20

2 Answers2

5

The compiler always makes the choice to use the most specific method.

In the first case, because the number of the arguments exactly matches void method(int x), it is the one that's being called.

In the second case, the number of the arguments don't match any case, and it can be called from both methods, resulting the ambiguity.

Check the JLS - 15.12.2. Compile-Time Step 2: Determine Method Signature for details.

Maroun
  • 94,125
  • 30
  • 188
  • 241
1

The number of arguments can be 0 or many. which makes

void method(int x, String... y){ } is similar to void method(int x){}

And the

void method(int... x){} is also similar to void method(int x){}

If you call method(1), the compiler will find two applicable method to this call and raises and exception.

Naili
  • 1,564
  • 3
  • 20
  • 27