Java1.5 adds new features: variable parameters: For cases where the parameter number is indeterminate and the type is determined, Java handles the mutable parameter as an array. Note: The mutable parameter must be in the last item. When the number of mutable parameters is one more, there must be one that is not the last, so only one variable parameter is supported. Because of the variable number of arguments, Java cannot differentiate whether the passed parameter belongs to the previous variable parameter or the back parameter, so only the variable parameter can be placed in the last item, when there is the same type parameter behind it.
Features of variable parameters:
(1), can only appear at the end of the parameter list;
(2) 、... Between the variable type and the variable name, there are no spaces before and after it can be;
(3), when invoking a variable parameter method, the compiler implicitly creates an array for the variable parameter, accessing the mutable parameter in the form of an array in the method body.
public class Varable {
public static void Main (String [] args) {
System.out.println (Add (2,3));
System.out.println (Add (2,3,5));
}
public static int Add (int x,int ... args) {
int sum=x;
for (int i=0;i<args.length;i++) {
Sum+=args[i];
}
return sum;
}
}
Java mutable parameters