Java record-78-variable parameters
Varargs allows programmers to declare a method that accepts variable parameters. Note: variable parameters must be the last parameter declared by the method. Variable parameters are essentially an array. For a declared variable parameter, we can either pass a discrete value or an array object. However, if we define the parameters in the method as an array, we can only pass the array object but not the discrete value. A variable parameter must be the last parameter of a method parameter, that is, a method cannot have two or more variable parameters.
Public class vararstest {public static int sum (int... nums) {int sum = 0; for (int num: nums) {sum + = num;} return sum;} public static void main (String [] args) {int ret = sum (1, 2, 4, 7); System. out. println (ret); ret = sum (1, 7); System. out. println (ret); ret = sum (new int [] {1, 7}); // int... it is equivalent to int [] System. out. println (ret );}}