A variadic function is one that accepts an indefinite number of parameters.
Let's see what the reason for these functions is within Java. Suppose we have a summation method that receives two integers and returns the sum of both.
public static int summation(int a, int b) { return a + b; }
If we want to add three numbers, we would have to overload the summation method to accept three parameters.
public static int summation(int a, int b, int c) { return a + b + c; }
What happens if we want to add four numbers? Again the summation method must be overloaded.
public static int summation(int a, int b, int c, int d) { return a + b + c + d; }
As we can see this is not scalable, since each time a different number of parameters is needed, the method would have to be overloaded again. At this point you could consider passing an array of integers as a parameter, but this does nothing more than wrap the actual parameters and make the method now depend on a new data type explicitly.
For these cases, variadic functions exist, to accept an indefinite number of parameters without the need to wrap them in another type of visible structure. It is important to consider that internally what Java does is create an array with the parameters that are passed to the variadic function, so we can work with methods of an array.
A variadic function is declared in the same way as any normal function, but as parameters the last or only parameter it has will have to comply with the following format: datatype... variablename. Within the way it is declared the only thing that changes is the addition of the three dots... after the data type. In some languages this type of parameter is known as varargs. Let's see how the summation method looks like a variadic function.
public static int summation(int... numbers) { int sum = 0; for (int number : numbers) { sum += number; } return sum; }
Now we can add any number of numbers without the need to overload the summation method, with the only restriction that they must all be of the same data type.
System.out.println(summation(1, 2)); // 3 System.out.println(summation(1, 2, 3)); // 6 System.out.println(summation(1, 2, 3, 4)); // 10 System.out.println(summation(2, 8, 16, 32, 64, 128, 256, 512, 1024, 2048)); // 4090
Before it was mentioned that internally Java handles the parameters of a variadic function as an array, that is, it wraps them within an array, and this allows us to work with methods of an array. For example, you can use the sum method of the Arrays class to add all the elements of the array within the summation method.
public static int summation(int... numbers) { return Arrays.stream(numbers).sum(); }
Considering the above, a variadic function can also be passed as a parameter an array of the type of data it accepts, no matter how curious it may seem. In this way Java no longer has to wrap the parameters in an array, since an array is being passed directly as an argument.
public static int summation(int a, int b) { return a + b; }
But what happens if the summation method for some reason needs a second parameter of type double, in addition to the n integers that it can already receive when declared as a variadic function. In this case, the summation method would have to be declared with the additional parameters it needs and at the end the variadic parameter or varargs, as shown below.
public static int summation(int a, int b, int c) { return a + b + c; }
In this way you can pass a double number as the first parameter and the integers that you want to add as additional parameters and Java will automatically know that the first parameter is of type double and the following are of type int, consider that the parameter variadic should always be the last parameter of the function, and not the first, since it would generate a compilation error, even the IDE itself would tell us the following: varargs parameter must be the last parameter.
public static int summation(int a, int b, int c, int d) { return a + b + c + d; }
In conclusion, if some utility function is required to perform some type of operations with an indefinite number of parameters and we do not want to pass an array or a list explicitly, the use of a variadic function can be considered. These are useful, they allow the code to be cleaner and more scalable, and they avoid method overloading.
The above is the detailed content of Variadic functions in Java. For more information, please follow other related articles on the PHP Chinese website!