Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

void... params meaning in java function declaration

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?

like image 268
rawatdeepesh Avatar asked Apr 15 '14 11:04

rawatdeepesh


People also ask

What is void function in Java?

The void keyword specifies that a method should not have a return value.

How do you declare a void variable in Java?

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.

What does params mean in Java?

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.

Can void methods have parameters in Java?

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.


6 Answers

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.

like image 200
akash Avatar answered Oct 02 '22 15:10

akash


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.

like image 20
Blackbelt Avatar answered Oct 02 '22 16:10

Blackbelt


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;
}
like image 28
Zohra Khan Avatar answered Oct 02 '22 15:10

Zohra Khan


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.

like image 22
Oleg Estekhin Avatar answered Oct 02 '22 16:10

Oleg Estekhin


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).

like image 38
kAnNaN Avatar answered Oct 02 '22 16:10

kAnNaN


it is variable length arguments

like image 29
Sanjay Rabari Avatar answered Oct 02 '22 15:10

Sanjay Rabari