Java Varargs explained with code samples
Java supports passing a variable number of arguments (varargs) to a method. In this article, I will be explaining this feature. What is varargs JDK 5 added the varargs feature, which allow creating a method that accepts a variable number of arguments. Prior to the varargs support, if you wanted to create a method with variable arguments, you either had to overload the method or use a Collection or array as a parameter. Varargs Syntax A variable number of arguments is specified using an argument name and three dots. The following code demonstrates this: void method myMethod(String ...str){//method body} This specifies a String argument str which is a vararg. So you can invoke this method as follows: myMethod("a"); //with 1 parametermyMethod("a","b"); //with 2 parametersmyMethod("a", "b", "c"); //with 3 parameters Code Sample The following code demonstrates a code sample that uses varargs: public class VarargDemo { public stat