Uncommon Java Syntax: Ellipses…
New programmers, and even some experienced ones, might not have experienced ellipses/varargs in Java yet. Here's a primer and some best practices.
Join the DZone community and get the full member experience.
Join For FreeExisting since Java SE 5.0 the ellipsis, also known as varargs, is one of those rarely underutilized features of Java. My guess is many novice programmers, and indeed even some experienced ones, have yet to meet Mr. Ellipsis — "…". I for one didn’t come across this elegant feature until after a year of full-time programming with Java. So what is an ellipsis?
Defining Varargs
I could not find any clear definition from the Javadocs but from what I could gather online, ellipses (also officially known as varargs (Variable Arguments)) are a Java syntax that describes an argument in a method that can take in zero or many arguments. Confusing? Let’s look at an example.
Ordinarily, if we wanted to define a method taking in two parameters of the same type, we would have something like this.
public int sum(int a, int b) {
Return a + b;
}
//A method to sum 10 numbers will look like this
public int sum(int a, int b, int c, int d, int e, int f, int g, int h, int I, int j) {
//return the sum of a to j
}
//Although a more experienced developer might decide to use an array like this
public int sum(int[] numbers) {}
Benefits
The downside to using an array here is that first, the numbers will first have to be stored in an array either through initialization or by iterating through a loop and assigning numbers to array elements.
Although it is still true that multiple arguments must be passed in an array, the varargs feature automates and hides the process for us, giving us a more elegant solution such as the one below:
public int sum(int… numbers) {
int sum = 0;
for (int number: numbers) {
sum += number;
}
return sum;
}
public static void main(String[] args) {
System.out.println(sum());
System.out.println(sum(1, 2));
System.out.println(int[] {
3,
4,
5
});
System.out.println(sum(6, 7, 8, 9, 10));
}
The output would be:
0
3
12
40
Notice in our third call of the method sum that we passed an array as argument for the method. This further proves that varargs still operate as an array behind the scenes.
Rules for Use
Before we run off to embrace this cool feature, there are some rules to its use we need to understand.
Rule 1: The Last Argument
Always use varargs as the last argument in a method. Using it as the first or middle arguments will create confusion and cause logical errors. For example:
public int sum(int… numbers, int y, int z) {
int sum = 0;
for (int number: numbers) {
sum += number;
}
return sum;
}
//Calling this method would look like this
int sum = sum(2, 3, 4, 5, 6);
From the above, which integers are part of the array and which two are for y and z arguments? It’s a contradiction that results in a compile-time error.
Rule 2: The One and Only Varargs
There must be not more than one varargs argument in a method. For example...
public int sum (int… numbers, String… names){}
...will lead to a compile-time error.
Rule 3: Do Not Overload
Varargs methods must not be overloaded. Otherwise, it creates confusion for the compiler, leading to a compile-time error. For example:
public int sum(int a, int… numbers){}
public int sum(int a, int b, int… numbers){}
In these examples above, rules 1 and 2 are not violated — but can you ascertain which method will be called?
sum(2,3,4,5);
sum(1,2,3,4,5);
This dichotomy leads to a compile-time error.
Now you know the pros and cons and do’s and don’t’s of using varargs. Enjoy!
Opinions expressed by DZone contributors are their own.
Comments