23

I'm unfamiliar with the parameter syntax in doInBackground(Params... params)

What is this type of thing called, and how do I utilize it?

dfetter88
  • 5,381
  • 13
  • 44
  • 55
  • 2
    This will give you a better understanding of how the params work: http://developer.android.com/reference/android/os/AsyncTask.html – dymmeh Jun 14 '11 at 11:59
  • Read the full description here : http://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3/6053673#6053673 – Kartik Domadiya Jun 14 '11 at 12:12

3 Answers3

59

As devA and VVV have said, that is called "varargs". Effectively, the following two lines of code are equivalent:

public void makeLemonade(String[] args) {

and

public void makeLemonade(String... args) {

the code inside the method would be the same, but when it was called, they would be called differently. The first would need to be called like this:

makeLemonade(new String[]{"lemon1", "lemon2", "lemon3"});

while the second one's method signature could have 0 to (an assumed)infinite number of arguments, but they would all need to be String arguments. All of the following calls would work:

makeLemonade("lemon1");
makeLemonade("lemon4", "lemon7", "lemon11", "lemon12"); 
makeLemonade();
// ... etc ...

A subtle difference between the two is that you can call makeLemonade() legally here if you're using varargs.

Travis
  • 3,737
  • 1
  • 24
  • 24
  • @Travis How do you indicate that no arguments are to be passed? Do you have to select a random type? – David Doria Sep 17 '13 at 15:24
  • To indicate that no arguments are to be passed, your method signature wouldn't include any parameters, so where above you see "public void makeLemonade(String... args)", you would instead use "public void makeLemonade()". This tells the compiler that no arguments are allowed. If you wanted to call the method with 0 arguments, that is perfectly valid, you would do just that, and call "makeLemonade()". – Travis Sep 18 '13 at 18:38
3

They are called Var Args.. Just like an array.. U can pass multiple items and just access like params[0].. etc..

ngesh
  • 13,398
  • 4
  • 44
  • 60
1

Params... params represents a vararg. It was added in J2SE5.0. It means you can pass any number of arguments or you can say array of arguments params[0]

Thanks Deepak

dmon
  • 30,048
  • 8
  • 87
  • 96
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243