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?
The void keyword specifies that a method should not have a return value.
Since JDK 1.1, Java provides us with the Void type. Its purpose is simply to represent the void return type as a class and contain a Class<Void> public value. It's not instantiable as its only constructor is private. Therefore, the only value we can assign to a Void variable is null.
Parameters. A parameter is a variable used to define a particular value during a function definition. Whenever we define a function we introduce our compiler with some variables that are being used in the running of that function.
2.4 Calling a Void Method With Parameters Now, we will discuss adding parameters to the void methods mentioned in the previous section. With parameters, the method works like a function in math—different input parameters can give different outputs, but the same set of parameters give the same behavior.
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;
}
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With