How does 'variable length arguments' work in Java ?

Thursday, July 3, 2014

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 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?







http://ift.tt/Vh8yVc