Variable-length parameters are provided starting from JAVA5, and variable-length parameters can be used as data
- Definition of variable length parameter method
Use... Represents variable-length parameters, such as
Print (String ... args) {
...
}
In a method with variable length parameters, parameters can be used as arrays, for example, all parameter values can be looped out.
Print (String ... args) {
for (String Temp:args)
SYSTEM.OUT.PRINTLN (temp);
}
- Invocation of a variable-length parameter method
Any number of arguments can be given at the time of invocation, for example:
Print ("Hello");
Print ("Hello", "John Doe");
Print ("Hello", "Zhang San");
- When invoking a method, if it can match the method of the fixed parameter and can match the method of variable length parameter, then Selecting fixed Parameters the method.
- A method can only be made up of a variable with an argument that is variable length and must be the last parameter.
- If the method to be called can match two mutable parameters, an error occurs when running, and the compilation can be
Java variable length parameter