What do reticence in the parameters of a method mean?

Asked

Viewed 3,512 times

48

I was looking at some methods of the package classes javax and some of them have signature similar to this:

protected void doInBackground(String... params){}

What does that mean " ... " ?

4 answers

52


What you have in the mentioned example is the setting that parameter params will receive the arguments in a string array, from Java 5.0+ (this definition is known as "varargs").

In other words, you can have a variable number of arguments in the function call from where the ....

This syntax can be used merged with "normal parameters":

void teste( String a, Integer... b ) {
    //...
}

teste( "a" );        //  a  recebe "a", b  fica vazio
teste( "a", 1, 2 );  //  a  recebe "a", b  recebe { 1, 2 }

And yet, if you want to use various types in the arguments:

void teste( String a, Object... b ) {
    //...
}

teste( "a", 1, "x", 7 );  //  a  recebe "a", b  recebe { 1, "x", 7 }

Note that in this case you are missing checking at build time on b by the fact that the object accepts different types in the arguments, and usually should make this check in Runtime to sanitize the values and use them with peace of mind.

  • Remembering that it can also be called an ellipse.

  • This is the same thing as String[] b?

  • 2

    @Skywalker essentially yes. But there’s no syntactic sugar in the call.

14

9

Note: that answer initially I gave in a question that was later identified as a copy of this question from here. Since the answers given in this question do not exemplify how to use varargs (just how to call), then I think this answer complements those of @Bacco and @Cold.

The ... is called Ellipsis or varargs, within teste, obj will have the same behavior as a vector. For example:

public void teste(Object... obj) {
    for (int I = 0; I < obj.length; i++)  {
      Object atual = obj[i];
      System.out.println(i + "-esimo elemento: " + atual);
  }
}

To call teste, however, you don’t need to instantiate a vector. You could do so:

teste("primeiro parâmetro", Integer.valueOf(1), null, BigDecimal.TEN);

1

The notation ... (three points, which in Portuguese is known as "ellipsis") is known in Java as varargs. It allows a method to accept an indefinite number of parameters of a given type.

In your case:

protected void doInBackground(String... params){}

This method accepts an indefinite amount of parameters of the type String.

On receiving this params in your method, you can read your content treating the params as any array. Thus, calling the method as follows:

doInBackground("Joao", "Pedro", "Paulo");

We will have:

protected void doInBackground(String... params){
    if (params.length > 0) {
         String paramZero = params[0];
         System.out.println(paramZero); // imprimirá 'João'
    }
}

Despite its flexibility, varargs has some limitations and risks:

  • Only a varargs is allowed by method;
  • The variable varargs must be the last parameter of the method;
  • Can create strange and unintuitive solutions, like this one from Slf4j, where he accepts a varargs from Object for the variables of the log message, but it checks, internally, if the last parameter inside the varargs is some kind of Exception, to log the same;
  • Situations of Overload involving varargs can go unnoticed in the code and cause unexpected situations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.