What is the difference between String... args
and String[] args
in Java?
I'm new to Java programming.
Can anyone plz tell me what's the difference between (String....args
) & (String [] args
) If I'll use first one in place of the second........Does it make any difference?
String... args
will declare a method that expects a variable number of String arguments. The number of arguments can be anything at all: including zero.
String[] args
and the equivalent String args[]
will declare a method that expects exactly one argument: an array of strings.
One difference that might not spring out from those observations is that in the second case (but not the first) the caller may have a reference to the array. In both cases the method works with args as an array of Strings, but if it does things like swap the array elements this will not be visible to the caller if the String... args
form is used.
There is no difference. And
String... args
is allowed.JLS-12.1.4. Invoke
Test.main
says (in part)Not explicitly listed (but also allowed)