-1

According to my understanding, a method with variable length argument and another method with array as an argument are interchangeable. for eg.

void test(int ... ints){} //method-1

is same as

void test (int [] ints){} //method-2

and we can't use both in the same class (Compile Time Error).

When I use method-1, I can call this method by passing some integers or by passing an array of integers since calling this method using some integers will implicitly create an array of those integers.

For eg.

test(1,2,3); //ok
test(new int[5]); //ok

But,

When I use method-2, I can't call this method by passing some integers as arguments.

test(new int[5]); //ok
test(1,2,3); //NOT OK

Questions:

1) If java implicitly creates an array of the arguments, why it is not able to call test(1,2,3) in case of method-2.

2) Why java doesn't allow using method-1 and method-2 together when both show different behavior?

Vikas Mangal
  • 821
  • 3
  • 10
  • 23
  • The second method expects an object of type `int[]` and not 3 individual primitive `int`s. – Maroun Jul 03 '14 at 11:15
  • [Can I pass an array as arguments to a method with variable arguments in Java?](http://stackoverflow.com/questions/2925153/can-i-pass-an-array-as-arguments-to-a-method-with-variable-arguments-in-java) is a "canonical" question about vararg vs array, with a lot of other vararg-related questions linked to it. – Oleg Estekhin Jul 03 '14 at 11:16
  • Var-arg methods are what is called "syntactic sugar" - the compiler converts the var-arg code to the array code, and so they generate the same (or almost the same) *class files*. They are not the same in the source code. – user253751 Jul 03 '14 at 11:18

3 Answers3

1

One difference I could think of is you can have additional arguments to a metod after an int[] but you cant have a variable argument like that. Variable argument should always be the last parameter to a method. Example

void test(int[] a, int a) {} // works fine.
void test(int ... ints, int a){} // compile error.
Syam S
  • 8,421
  • 1
  • 26
  • 36
0

1) If java implicitly creates an array of the arguments, why it is not able to call test(1,2,3) in case of method-2.

Because 1,2,3 in test(1,2,3) is not an array, these are three different integers...You have to pass an Array of int to invoke method-2.

2) Why java doesn't allow using method-1 and method-2 together when both show different behavior?

It creates ambiguity when you call method with array as an argument because array as an argument is allowed in case of varargs as well as array as an argument.

for example, if both method were allowed, test(new int[5]) will create an ambiguity.

Not a bug
  • 4,286
  • 2
  • 40
  • 80
0

The internal representation in the Java compiler of the function declarations in these two cases is identical. That's why the compiler wont let you declare two such functions with the same name.

The difference in the bahivour of these two declarations can be seen when you actually call the functions with the actual arguments. Since Java is a strongly typed language (with strict type checking) it doesn't allow you to pass three integers to the test (int[] a) method.

However, when you call test(int ... a) with test(1,2,3) the compiler generates intermediate code to make a temporary array of three integers and passes it onto the function.

Debasis
  • 3,680
  • 1
  • 20
  • 23