Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between String... args and String[] args in Java? [duplicate]

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.

like image 598
user3367304 Avatar asked Jun 20 '15 02:06

user3367304


1 Answers

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"}).

like image 142
Evgeniy Dorofeev Avatar answered Oct 15 '22 08:10

Evgeniy Dorofeev