for example I have a code like this: (from here)
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {}
@Override
protected void onPostExecute(String result) {}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {
}
}
and what do the 3 dots in the parameter of the method do?
- Stack Overflow What do 3 dots next to a parameter type mean in Java? What do the 3 dots following String in the following method mean? It means that zero or more String objects (or a single array of them) may be passed as the argument (s) for that method.
Generics means parameterized types. The idea is to allow type (Integer, String, … etc, and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as class, interface, or method that operates on a parameterized type is called ...
We can use the 3 dots in JavaScript function calls to convert an array into a set of arguments for a function. Let’s look at an example. Below, our array is converted into the values for x, y, z and a.
The common type parameters are as follows: Like the generic class, we can create a generic method that can accept any type of arguments. Here, the scope of arguments is limited to the method where it is declared. It allows static as well as non-static methods. Let's see a simple example of java generic method to print array elements.
The three dots are referred to as varargs
and here, allow you to pass more than one string to the method like so:
doInBackground("hello","world");
//you can also do this:
doInBackground(new String[]{"hello","world"});
Documentation on that here.
Within the method doInBackground
you can enumerate over the varargs variable, params
like so:
for(int i=0;i<params.length;i++){
System.out.println(params[i]);
}
So its basically an array of strings within the scope of doInBackground
The compiler treats the three dots ...
as taking in an array of that object. In this case String
and Void
. The amount of objects you pass in is the size of the array.
Thus:
doInBackground("Hi", "Hello", "Bye")
will create an array of String
of length 3.
This concept is called varargs and explained here
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