Java1.5 adds a new feature: variable parameters. If the number of parameters is uncertain and the type is determined, java treats variable parameters as arrays.
Note: The variable parameter must be in the last one. When there is one more variable parameter, one is not the last one, so only one variable parameter is supported.
Because the number of parameters is not fixed, when there are parameters of the same type at the end of the parameter, java cannot distinguish whether the input parameter belongs to the previous variable parameter or the latter parameter. Therefore, the variable parameter can only be placed in the last one.
Package com. ljq. test;
/**
* Variable Parameter usage
*
* @ Author jiqinlin
*
*/
Public class VariableParameter {
Public static void main (String [] args ){
System. out. println ("Result:" + sum (1, 2, 3, 4 ));
}
Private static int sum (int... params ){
Int result = 0;
For (int param: params ){
Result + = param;
}
Return result;
}
}