2

The following code compiles fine in Java:

public static void main(String[] args) {
    int i =5;
    call(i);
}


static void call(int i){
    System.out.println("int");
}

static void call(long i){
    System.out.println("long");
}

static void call(Integer i){
    System.out.println("Integer");
}


static void call(Object i){
    System.out.println("Object");
}

But the following code gives compile time error:

public static void main(String[] args) {
    int i =5;
    call(i);
 }


static void call(int... i){
    System.out.println("int...");
}

static void call(long... i){
    System.out.println("long...");
}

static void call(Integer... i){
    System.out.println("Integer...");
}


static void call(Object... i){
    System.out.println("Object...");
}

Why the similar call mechanism is not maintained by Java while working with var-args? In the second example also, the call should go to method static void call(int... i)

Kumar
  • 1,536
  • 2
  • 23
  • 33
  • Does it work without the `Integer` and `Object` versions? If yes, then it's because of autoboxing. – NilsH Apr 30 '13 at 05:38
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2521293/bug-with-varargs-and-overloading – c.P.u1 Apr 30 '13 at 05:47
  • Correct, @c.P.u1. However, that should be fixed with recent jdk 7 versions, so I assume this particular error is because of autoboxing. – NilsH Apr 30 '13 at 06:08
  • Here's an explanation: http://www.xyzws.com/Javafaq/why-overloading-a-varargs-method-doesnt-work-for-the-primitive-type-and-its-object-wrapper-type/50 Morale: Don't have both primitive and reference types in overloaded varargs methods. – NilsH Apr 30 '13 at 06:26

4 Answers4

1

Here is answer

Java doesnot work well with overloading varargs method.

Here is what Specs provide :

So when should you use varargs? As a client, you should take advantage of them whenever the API offers them. Important uses in core APIs include reflection, message formatting, and the new printf facility. As an API designer, you should use them sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.

  • You can overload vararg methods. But in some cases, you might run into edge cases, like the OP has. – NilsH Apr 30 '13 at 06:09
  • 1
    And here's an explanation of what's actually happening: http://www.xyzws.com/Javafaq/why-overloading-a-varargs-method-doesnt-work-for-the-primitive-type-and-its-object-wrapper-type/50 – NilsH Apr 30 '13 at 06:28
  • That wording is from the *[Guide to Features,](http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html),* not the Java Language Specification. – user207421 May 01 '13 at 11:45
0

As you are declaring the method as Static void call(int... i) and the method expects the int array but while calling this method you are sending only one integer value.

Static void call(int... i) is Same as Static void call(int[] i)
Vasu
  • 149
  • 7
-1

Variable arguments are treated as arrays in java. So instead of passing an int value, pass it like an array . For eg.

int[] i ={5};
call(i);
Vineet Singla
  • 1,609
  • 2
  • 20
  • 34
  • 1
    Yes, they are treated as arrays, but you don't have to call them with arrays. – NilsH Apr 30 '13 at 06:00
  • In the current scenario, this is the way I suppose the method can be called. Or else you could do.. call(5,6,7). So my solution was pertaining to the problem, so that the person can understand how to call on this... Its not always right to give a negative vote without understanding the intention of the solution – Vineet Singla Apr 30 '13 at 06:03
  • 1
    Yes, but it doesn't answer the question, which is, why it is giving an error. – NilsH Apr 30 '13 at 06:04
-1

The link given by @NilsH explains the reason clearly. Please find the details at http://www.xyzws.com/Javafaq/why-overloading-a-varargs-method-doesnt-work-for-the-primitive-type-and-its-object-wrapper-type/50

Kumar
  • 1,536
  • 2
  • 23
  • 33
  • It's usually best not to give an answer is only a link, as the link may go 404. Can you give a summary of the link here? – james.garriss Dec 23 '13 at 14:52