Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the 3 dots in Java generics mean? [duplicate]

Tags:

java

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?

like image 995
maysi Avatar asked Jul 12 '13 20:07

maysi


People also ask

What do 3 Dots next to a parameter type mean in Java?

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

What is generics in Java?

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

How to use the 3 dots in JavaScript function calls?

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.

What are the common type parameters of generic class in Java?

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.


3 Answers

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

like image 151
William Morrison Avatar answered Oct 31 '22 08:10

William Morrison


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.

like image 22
chancea Avatar answered Oct 31 '22 08:10

chancea


This concept is called varargs and explained here

like image 21
Michael Lang Avatar answered Oct 31 '22 08:10

Michael Lang