• Post Reply Bookmark Topic Watch Topic
  • New Topic

Varargs - Why does this code compile?  RSS feed

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry this is a crosspost because I wasn't able to get an answer from the other forum. Here is my question:


Can someone please tell me why this code compiles? I thought it must give an error because of an ambiguous call to varargs(), but it prints "Hello1" when varargs(1 ) is called.

package test;

public class Test {

public static void varargs(int x, int ...v) {
System.out.println("Hello2");
}
public static void varargs(int x) {
System.out.println("Hello1");
}
/**
*
* @param x
* @param y
*/


public static void main(String[] args) {
System.out.println("Main method");
varargs(1);
}
}

How does the program distinguish which method to call? Since both methods take 1 int, I could be calling either of them (because varargs can take no arguments also).
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the JLS the compiler will choose the "most specific" method. There is also advice somewhere (can't find a reference to it now) not to overload with varargs for the very reason you are posting - it isn't always immediately clear which method will be called.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. But the below code does give me the compile error I was expecting the other code to throw.

package test;
class Main
{
static void varArgs(int ...i)
{
System.out.println("Hi");
}

static void varArgs(int h, int ...i)
{

}
public static void main(String args[])
{
varArgs(1);
}
}

The error here "reference to varArgs is ambiguous". Why does the error show up here but not in the other code? Maybe because here both methods use varargs and in the other code, only one uses it?
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:
The error here "reference to varArgs is ambiguous". Why does the error show up here but not in the other code? Maybe because here both methods use varargs and in the other code, only one uses it?


I think you already answered your question. If you pass 3 ints which method would the complier use? You could argue that the second is "more specific" but it is a fine distinction. I'm sure the answer is in the JLS but I think the best approach is just to accept that overloading with varargs is to be avoided with the added bonus that you can also avoid reading the JLS.


More specifically, how does the compiler know if you are calling the first method with 3 int i varargs or the second method with int h and two int i varargs?
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!