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 " ...
" ?
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 " ...
" ?
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.
14
Named for varargs
, is a feature/capability allows a method to receive multiple or no argument of the same type.
The ... doInBackground(String... params)
means that this method can receive no parameter or several of the type String
.
More details:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
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:
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;Browser other questions tagged java
You are not signed in. Login or sign up in order to post.
Remembering that it can also be called an ellipse.
– user2692
This is the same thing as String[] b?
– Skywalker
@Skywalker essentially yes. But there’s no syntactic sugar in the call.
– Maniero