Why can't we use arrays instead of varargs?

I just stumbled upon varargs while studying android ( doInBackground(Type... params) ), SO posts clarified its use

My question is: why can't we use arrays instead of varargs

 public void foo(String...strings) { } 

I can replace this type of call by packing a variable number of arguments into an array and passing it to a method, for example

 public void foo(String[] alternativeWay){ } 

Also used main(String[] args) in java use varargs , if not, how can we pass execution parameters to it

Please suggest benefits or use of varargs and is there anything else important you need to know about varargs

+6
source share
4 answers

The only difference between

 foo(String... strings) 

and

 foo(String[] strings) 

for the calling code. Consider this call:

 foo("a", "b"); 

This is valid with the first declaration of foo , and the compiler will emit code to create an array containing references to "a" and "b" at run time. This is not valid with the second declaration of foo , though, since it does not use varargs.

In any case, itโ€™s enough for the caller to explicitly create an array:

 for(new String[] { "a", "b" }); // Valid for either declaration 

Also main (String [] args) in java uses varargs, if not the way we can pass execution parameters to it

When it is written as main(String[] args) , it is not; if you write main(String... args) then this will happen. This has nothing to do with the way the JVM treats it, because initializing the JVM creates an array with command line arguments. This will only matter if you write your own code to explicitly call main .

+11
source

We can use arrays instead of varargs. Varargs is syntactic sugar for using arrays. But they make your code more compact and readable. Compare

 private void foo(String... ss) { ... } private void bar() { ... foo("One", "Two", "Three"); ... } 

with

 private void foo(String[] ss) { ... } private bar() { ... foo(new String[] { "One", "Two", "Three" }); ... } 

Similarly, we do not need a diamond operator (<>, Java 7) or lambdas (Java 8). But they make the code more readable and therefore more convenient.

+4
source

One of the benefits of vargars is methods that require at least one parameter, for example max . With varargs you can do it like this

 static int max(int first, int... remaining) { int max = first; for (int number : remaining) max = Math.max(max, number); return max; } 

This is great because it is not possible to pass any parameters to the max method, and the call code for max really clean: max(2, 4, 1, 8, 9) . Without varargs, the only way to enforce a condition by which at least one number must be passed would be an exception at run time if the array was 0 (it is always best to avoid) or force the caller to write max(2, new int[] {4, 1, 8, 9}) , which is really ugly.

+1
source

Since you are making a function call, this is more like a function call, for example:

 new MyAsyncTask().execute("str1", "str2"); 

looks better:

 new MyAsyncTask().execute(new String[]{"str1", "str2"}); 

There AsyncTask no magic behind AsyncTask , very often you do not need to pass any parameters, sometimes you pass parameters to the constructor instead of executing. There are also AsyncTask implementations:

https://github.com/roboguice/roboguice/blob/master/roboguice/src/main/java/roboguice/util/SafeAsyncTask.java

who do not use varargs at all

+1
source

Source: https://habr.com/ru/post/980255/


All Articles