As the name suggests, the variable length parameters refer to the parameters of the method, as long as it is defined, we can use any number of parameters, similar to the use of an object array. In J2SE 5.0, a new syntax is introduced, which is after the parameter type name is added ..., indicating that the method can accept multiple parameters of this type. It should be noted that the variable length parameters must be placed at the end of the parameter list, and one method can only contain one such parameters. It should be noted that this syntax is just when a method is declared, and when we do this method, we usually don't write, because this parameter is only used as an Object when it is written. Call access to the meaningful object array through this Object method. So usually we use the syntax when declaring a method, while using an array during specific implementation. The compiler will handle this difference and sign them as the same method. Look into the example below: Interface itestvarargs {public void vararg (Object ... args);
Class Myvarargs Implements ITESTVARARGS {public void vararg (object [] args) {// ...}} There is also a very important new feature based on variable length parameters, that is, formatted output, I Will be described in more detail later. More detailed descriptions of variable length parameters, refer to here.
[Update]
Corrected: I have a problem with Object ... such a form of signature, in fact, if a method is signing similar to Object ... args, in the method body, Args is treated as an Object array, so that Object [] The same manner, such as Args.Length or Args [0], etc. So it seems that this code should be more appropriate:
Interface ITestVarargs {public void vararg (Object ... args);
Class MyvaRgs Implements Itestvarargs {Public Void Vararg (Object ... ARGS) {// ...}}
Laugh.