I have seen a method that is written as follows.
public void metodox(int ... args) {
}
I tried passing parameters to it and it accepts N number of parameters.
metodox(1,2,3,4);
metodox(1,2);
I have seen a method that is written as follows.
public void metodox(int ... args) {
}
I tried passing parameters to it and it accepts N number of parameters.
metodox(1,2,3,4);
metodox(1,2);
It stands for varargs and is used to pass 0 or more parameters of the same type. For example, the method you indicate:
Supports that you can pass 0, 1 or more type arguments
int
only.varargs can only be used as the last type of parameter to use in a method. For example:
It means that the first argument must be a
String
and the others (0, 1 or more) are of typeint
.In case you put a varargs type variable, no variable definition can go after this. Example:
Java interprets varargs parameters as an array. For example, an implementation for the method would be:
Therefore, it also supports that instead of sending multiple arguments, you can send an array instead. Example:
Departure:
Important : Keep in mind that if you have the method overloaded, the method that varargs uses will be the last option the compiler chooses to check your code, just as it will be the last option the JVM uses in case you call one of the overloaded methods.
Example:
Departure:
It means that it is an argument variable, it is similar to an array ( [] ) and it supports sending 0 or more parameters separated by a comma (,)
Example :
I hope it helps you
Greetings.