9

When programming for android, whenever I use an AsyncTask the doInBackground method looks like this.

 protected String doInBackground(String... args)

But when using the arguments anywhere in that block I can access them like a normal String array for example in my program

        @Override
    protected String doInBackground(String... args)
    {
        String details = "";
        try
        {
            details = facade.getRecipeDetails(args[0]);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return details;
    }

Which works fine and I have no problem dealing with it. But I'm wondering why they use (String . . . args) instead of a normal array of Strings. Is it just because in the calling method you can just write something like:

 new AsyncHandler.execute("argument1","argument2","argument3",...)  

instead of creating a new array to pass the arguments? Though we could write

new AsyncHandler().execute(new String[]{"Argument1","Argument2"});

which is a bit more verbose.

Are (String ...) and String[] synonymous in how they work, but the argument passing is just easier using the former because there is no need to create an array? As far as I can tell, the former also gets translated to a string array in the background so would they both compile to the same code and it's just 'syntactic sugar'?

  • (String ...) means you can add as much String param as you want, String[] is one parameter which is an array of strings. (String ...) depends upon the first param of AsyncTask, it is not always String. – FaizanRabbani Dec 28 '14 at 18:14
  • @FaizanRabbani except that you can also call a String... as if it's String[] – ratchet freak Dec 28 '14 at 18:17
  • @ratchetfreak Yes it can act as a String[], but lets assume it was by default String[]. It would have been quite a problem to use different String param in an array. Instead they gave a flexibility to use (String ...). – FaizanRabbani Dec 28 '14 at 18:20
  • @FaizanRabbani I'm aware that it's not always String, it's just an example =). Same goes for Boolean ... or other things, but I liked to use String due to the main(String[] args) we're familiar with. And you said 'you can pass as much strings as you want', well you can pass quite a few strings with an array. To me it still seems like you can achieve the same with arrays, though it's less readable and more of a hassle. (Passing an array of arrays for example, would reduce readability a bit). Is it correct that it's mainly for readability then? (thanks already btw) – Dylan Meeus Dec 28 '14 at 18:25
  • @FaizanRabbani well it's a simple wrap of new String[]{...} to convert (it's what actually happens when you use a String...) – ratchet freak Dec 28 '14 at 18:25
15

(String ...) is an array of parameters of type String, where as String[] is a single parameter.

Now here String[] can full fill the same purpose here but (String ...) provides more readability and easiness to use.

It also provides an option that we can pass multiple array of String rather than a single one using String[].

  • 1
    While String[] can be passed into a method taking String... as a parameter, two String[]s cannot. There is no implicit array concatenation that takes place here. – user22815 Dec 29 '14 at 15:55
5

A feature of String[] vs String... is that the "String..." does not need to be part of the call.

public void test(String... args){ 
   if(args.length > 0){
      for( String text : args){
         System.out.println(text);
      }
   }else{
      System.out.println("No args defined.");
   }
}

public void callerTest(){
     test();
     System.out.println();

     test("tree");
     System.out.println();

     test("dog", "cat", "pigeon");
}

Then if you call callerTest(); the Output will be:

No args defined.

tree

dog
cat
pigeon

protected by gnat Apr 11 '18 at 20:58

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.