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.
If you have void x(String...args)
method, you can call it as x("1", "2", "3")
while void x(String[] args)
can be called only as x(new String[] {"1", "2", "3"})
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With