Question

I recently came across a java snippet .The function definitions have a different format than what i know till now . Following are the codes-

  protected Void doInBackground(String... params) {
        Log.i(TAG, "doInBackground");
        //Invoke web method 'PopulateCountries' with dummy value
        invokeJSONWS("dummy","PopulateCountries");
        return null;
    }

and a similar function with ... in the parameter

protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }

What does ... mean in the following context?

Was it helpful?

Solution 4

Android's AsyncTask is a generic type.

When you need an async task that has no sense of intermediate progress data, you should declare it as MyTask extends AsyncTask<Something, Void, Something> using the class Void as the specification of the Progress type variable, and, following usual generic rules, if you decide to overwrite onProgressUpdate you will have to declare it as onProgressUpdate(Void... values).

Hence Void... does not mean anything other than the usual vararg method whose type happened to be Void.

OTHER TIPS

What does ... mean in the following context .

This specifies variable length argument when you want to call method with parameters of type say String but you don't know how many parameters you want to pass you can use this.

as you can pass any number of String to method.

So you can call method like these ways.

for

public void met(String...a)

you can call this method by

ob.met()
ob.met("a")
ob.met("a","b")

and so on.

You can read more about it HERE.

it called varargs, and it means an arbitrary number of parameter of the same type. You can access it on per index basis, like an array.

type ... variableName

The ellipsis (...) identifies a variable number of arguments, and is demonstrated in the following summation method.

static int sum (int ... numbers)
{
   int total = 0;
   for (int i = 0; i < numbers.length; i++)
        total += numbers [i];
   return total;
}

There is optional parameters with Java 5.0. Just declare your function like this:

public void doSomething(boolean...optionalFlag) {
    ...
}

you could call with doSomething() or doSomething(true) now.

This is "new" in Java 1.5 and beyond (not supported in Java 1.4 or earlier).

it is variable length arguments

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top