What does the syntax "final String ... args" / do mean?

This should be a fairly simple question to answer. I looked around and could not find a single topic of this syntax, and "..." makes it difficult to search on Google. I am working on a simple test application to copy a database file from its secure location on an unmanaged Android phone to a location on the SD card that I can get to view using the sqlite3 database viewer tool . I know this seems like a roundabout way of doing something, but the emulator refuses to open on my netbook, so I am using my mobile phone to test the development right now.

The code is already written, so I borrowed it here and adapted it to my code. I came across this small piece of code:

private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> { private final ProgressDialog dialog = new ProgressDialog(ManageData.this); // can use UI thread here protected void onPreExecute() { this.dialog.setMessage("Exporting database..."); this.dialog.show(); } // automatically done on worker thread (separate from UI thread) protected Boolean doInBackground(final String... args) { 

I have never seen the final String... args argument before. What does this mean / do?

Thanks! Moscro

+6
java android variadic-functions
source share
2 answers

Ellipses in an argument in Java mean vararg of this type. So, in your case ... means that you can pass any number of String parameters to doInBackground methods.

So you can call this method doInBackground("String1", "String2", "String3") , as well as doInBackground("String1") , as well as doInBackground("String1", "String2", "String3", "String4") or any other form.

See here http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

Three periods after the final parameter type indicate that the last argument can be passed as an array or as a sequence of arguments. Vargars can only be used in the final position of the argument. Given the new varargs declaration for MessageFormat.format, the above call could be replaced by the next shorter and sweeter call

and final has the usual meaning. BTW, this feature is available in Java5 or later.

Contact: http://www.developer.com/java/other/article.php/3323661

As mentioned in the two links above, this is nothing but a sugared array. Basically, when you define myMethod(T... vararg) , Java sees it as myMethod(T[] vararg) . And when you call myObj.myMethod(t1, t2, t3, ..., tn) , Java passes it as myObj.myMethod(new T[]{t1, t2, t3, ..., tn}) .

+11
source share

final means args cannot be reassigned to doInBackground . String... is vararg . "You can pass any number of String objects to the doInBackground method.

String... really is String[] - it is literally an array of String and can be accessed as such.

+4
source share

All Articles