Why can't we just use arrays instead of varargs?

假装没事ソ 提交于 2019-12-22 04:01:57

问题


I just came across varargs while learning android(doInBackground(Type... params)) ,SO posts clarified the use of it

My question is why can't we just use Arrays instead of varargs

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

I can replace this type of a call by packing my variable number of arguments in an array and passing it to a method such as this

public void foo(String[] alternativeWay){  }

Also does main(String[] args) in java use varargs , if not how are we able to pass runtime parameters to it

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


回答1:


The only difference between

foo(String... strings)

and

foo(String[] strings)

is for the calling code. Consider this call:

foo("a", "b");

That's valid with the first declaration of foo, and the compiler will emit code to create an array containing references to "a" and "b" at execution time. It's not valid with the second declaration of foo though, because that doesn't use varargs.

In either case, it's fine for the caller to explicitly create the array:

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

Also does main(String[] args) in java use varargs , if not how are we able to pass runtime parameters to it

When it's written as main(String[] args) it doesn't; if you write main(String... args) then it does. It's irrelevant to how the JVM treats it though, because the JVM initialization creates an array with the command line arguments. It would only make a difference if you were writing your own code to invoke main explicitly.




回答2:


We could use arrays instead of varargs. Varargs are syntactic sugar for using arrays. But they make your code more compact and more 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 don't need the diamond operator (<>, Java 7) or lambdas (Java 8) either. But they do make code more readable and therefore more maintainable.




回答3:


One advantage of varargs is for methods requiring at least one parameter, such as 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 impossible to pass no parameters to the max method, and the calling code for max is really clean: max(2, 4, 1, 8, 9). Without varargs the only way to have enforced the condition that at least one number should be passed would have been to have thrown an exception at runtime if the array had length 0 (always best avoided) or to force the caller to write max(2, new int[] {4, 1, 8, 9}) which is really ugly.




回答4:


Because you function call looks more like a function call, ex.:

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

looks better than:

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

There is no magic behind AsyncTask, very often you dont really need to pass any parameters, sometimes you pass parameters to constructor instead of execute. There are also implementations of AsyncTask :

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

that dont use varargs at all



来源:https://stackoverflow.com/questions/27689220/why-cant-we-just-use-arrays-instead-of-varargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!