Java Varargs | Variable arguments in Java
The Variable-Length Arguments or varargs
in Java was introduced in Java 1.5. Either the method was overloaded or the arguments were passed in an array to the method before the varargs
feature was introduced. Though it might be easy to overload a method or pass the arguments as an array. But when the number of arguments scales up, the code might be difficult to maintain and it might increase complexity. This article will explain the Variable-Length Arguments in Java with an example.
varargs
method in Java
A variable-length argument can have 0 or more values. A method containing a variable-length argument is called a varargs method or variable arity. A varargs
method should have the following signature,
access_modifier return_type method_name(type parameter1, type parameter2, type... variable_length_parameter_name) { // statements }
- A variable-length argument must be in the last position if the method has other parameters
- A
varargs
method must have only one variable-length argument. - In the method signature, after the type of the
varargs
three dots or periods or an ellipsis (...
) should be used to denote the compiler that it is a variable-length argument.
Using varargs
method wisely can reduce the number of overloaded methods. Therefore code size can be reduced. It is recommended to use varargs
only when the number of arguments is unknown and use overloading otherwise.
An Example
Consider the following Java program that uses varargs
,
class varArgsExample { private static void greetGuests(String... a) { int length = a.length; if (length == 0) { System.out.println("My hearty welcome to everyone!!"); } else { for (String guestName: a) System.out.println("Welcome "+guestName); } } public static void main (String[] args) { String[] names = {"Mrs. Tina", "Mr. Rohan", "Mr. Rana", "Mrs. Nina", "Mr. Gaurav"}; greetGuests("false"); greetGuests(names); greetGuests("Mrs. Tina", "Mr. Rohan", "Mr. Rana", "Mrs. Nina", "Mr. Gaurav"); } }
In the above example, if the second and third calls to greetGuests()
are commented, the program gives the following output
My hearty welcome to everyone!!
This call to the greetGuests()
method takes no arguments and the length of the formal parameter is 0. So the statement in if
block is executed.
Now, commenting the first and third calls to the greetGuests()
method, the following output is printed.
Welcome Mrs. Tina Welcome Mr. Rohan Welcome Mr. Rana Welcome Mrs. Nina Welcome Mr. Gaurav
This call to the greetGuests()
takes an array of 5 strings as the only argument. Actually, this was the method used before the varargs
was introduced. The length of the array is 5 and so the print statement in the else
block is executed.
Finally, commenting the first and the second calls to the greetGuests()
method, the following output is printed, which is the same as the previous output.
Welcome Mrs. Tina Welcome Mr. Rohan Welcome Mr. Rana Welcome Mrs. Nina Welcome Mr. Gaurav
This call to the greetGuests()
method takes 5 strings as the arguments. Since the length of the formal parameter is 5, the statement in the else block is printed. While converting the source code to byte code, the compiler matches the formal parameters from left to the right with the actual parameters. When the varargs
parameter is encountered, it is converted to an array and passed to the method along with the other parameters. This is the reason why this output is the same as the previous one.
Overloading a varargs method
Similar to other methods, a varargs
method can also be overloaded. It is not recommended because it might lead to ambiguity in some cases. Suppose the class varArgsExample had another method,
private static void greetGuests(String flag, String... a) { int length = a.length; if (flag.compareTo("true")==0) { if (length == 0) { System.out.println("My hearty welcome to everyone!!"); } else { for (String guestName: a) System.out.println("Welcome "+guestName); } } else { if (length == 0) { System.out.println("Thank you everyone!!"); } else { for (String guestName: a) System.out.println("Thanks "+guestName); } } }
Invoking the method greetGuests()
like greetGuests("true")
is ambiguous. Since this call matches both the method definitions, the compiler throws an error. So it is not recommended unless there is an utmost need to overload vararg
methods.
Leave a Reply