Java | var-args methods
A wonderful feature introduced in java version 1.5
var-args methods (variable number of arguments methods)
In Java version 1.5, Java has included a feature that simplifies the creation of methods that need to take a variable number of arguments. A method that takes a variable number of arguments is a var-args method.
Prior to Java version 1.5, it could be handled bytwo ways. One using overloaded method(one for each) and another put the arguments into an array, and then pass this array to the method. Both of them are potentially error-prone and require more code. The varargs feature offers a simpler, better option.
Prior to Java version 1.5
Here we need to overload the methods to perform the sum operations for different number of arguments. It requires more code.
From Java version 1.5
To solve the above problem, Java comes with var-args method.
Defining var-args method → func(int… x)
JVM internally converts the arguments and stores it in an array. Hence we can access the passed values by indexing.