Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This should be a fairly simple question to answer. I looked around and couldn't find any topics on this syntax, and the "..." makes searching for it difficult on Google. I'm working on a simple test application for copying a database file from its protected location on an un-rooted Android phone to a place on the SD Card that I can access for viewing with the sqlite3 database viewing tool. I know this seems like a roundabout way of doing things, but the emulator refuses to open on my netbook, so I'm using my cell phone to test the development for right now.

The code has already been written, so I'm borrowing it from here and adapting it to my code. I've come across this little snippet 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've never seen the argument final String... args before. What does this mean/do?

Thank you! Moscro

like image 491
moscro Avatar asked Feb 26 '11 05:02

moscro


2 Answers

The ellipses in the argument in Java means vararg of the type. So, in your case, ... means you can pass any number of String parameters to the doInBackground methods.

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

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

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position. Given the new varargs declaration for MessageFormat.format, the above invocation may be replaced by the following shorter and sweeter invocation

and final has it's usual meaning. BTW, this feature is available in Java5 or newer.

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

As the above two links mentions, it's nothing but 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}).

like image 127
Nishant Avatar answered Oct 19 '22 16:10

Nishant


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

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

like image 25
Jonathon Faust Avatar answered Oct 19 '22 17:10

Jonathon Faust