The implementation of the variable parameter function in Java is actually an array, and its simple usage is as follows
public class Variableparamtest {
private static void Variableparam (Object ... args) {
for (Object V:args) {
System.out.print (v.tostring () + "");
}
System.out.println ();
}
private static void VariableParam2 (int a, int ... args) {
System.out.println ("A:" + a);
System.out.print ("Variable params num:" + args.length);
}
public static void Main (string[] args) {
Variableparam (1, 2.0F, 3L, ' Z ');
VariableParam2 (' A ', 2, 3, 4);
}
}
1 2.0 3 Z
A:65
Variable params num:3
The Java-based root class is object, and we can define the variable function type as object so that all different types of variable-length parameters can be supported, and the Java arguments provide automatic wrapper functionality, such as Int->integer, Float->float, and so on.
In addition, we can combine the arguments with the non-variable parameters, such as variableParam2 () in the previous example, and the value of the argument function is allowed to be null.
Implementation of the Java variable parameter function