Varargs (variable arguments) variable long parameters are features introduced in Java 1.5.
The form of the method is shown as print (String ... s), which is an arbitrary number of values.
public class varargsdemo{public static void print (string ... s) {for (string a:s) System.out.print (a); } Public static void Main (string[] args) { print ("a", "\ n"); Print ("A", "B", "\ n"); Print ("A", "B", "c\n"); } }
Output:
A
Ab
Abc
Working principle:
1. Create an array with the length of the number of parameters passed by the caller;
2. Put the values of the arguments into the array,
3. Pass the Array (reference) to the modulated method.
Examples:
String.Format (String format, Object ... args)
The reflection mechanism.
"There are a lot of APIs in 7.3.2 that use variable-length parameters. I do not remember the introduction of programming, said the variable length of the number of references no, add the first "
Attention:
- The variable length of the reference is to be the final form of the method;
- Avoid overloading compilation errors.
Java variable length number of references varargs