As the name implies, variable length parameters refer to any number of parameters in the parameter body of a method. They are similar to object arrays. In J2SE 5.0, a new syntax is introduced, that is, adding... after the parameter type name indicates that this method can accept multiple parameters of this type. It must be noted that the variable length parameter must be placed at the end of the parameter list, and a method can only contain one such parameter.
Note that this syntax only works when declaring a method. When we implement this method, we usually do not write it like this, in this case, this parameter is used only as an Object in the method body. You cannot call this Object method to obtain an array of meaningful objects. Therefore, we usually use such a syntax when declaring a method, while using arrays in specific implementations. The compiler will handle this difference and treat them as signatures of the same method. See the following example:
Interface ITestVarargs {
Public void vararg (Object... args );
}
Class MyVarargs implements ITestVarargs {
Public void vararg (Object [] args ){
//...
}
}
In J2SE 5.0, another important new feature is based on variable length parameters, that is, formatting the output. I will explain it in more detail later.
For more information about variable length parameters, see here.