In this article, we will discuss about passing variable number of arguments to Java methods. Variable arguments feature was introduced in Java 1.5 and it is a very useful feature as we will see in the below examples.
Also Read:
Prior to Java 1.5
Lets say we have an example as has been illustrated below. It is a very basic example having three different methods for the same functionality based on the number of inputs provided.
/** * This class is to illustrate the basic need for var arguments feature introduced in Java 1.5 */ public class NoVarArgs { public static void main(String[] args) { /* Assume that you need to perform same operation but user can supply different number of parameters */ int sub1 = subMethod1(30,10); int sub2 = subMethod2(30,20,10); int sub3 = subMethod3(30,20,3,2); System.out.println("Subtract method 1:" + sub1); System.out.println("Subtract method 2:" + + sub2); System.out.println("Subtract method 3:" + sub3); } public static int subMethod1(int a, int b) { return a - b; } public static int subMethod2(int a, int b, int c) { return a - b - c; } public static int subMethod3(int a, int b, int c, int d) { return a - b - c - d; } }
Can’t we do it better? Of course, there are alternatives. We could use an array shown as below:
/** * This class is to illustrate the basic need for var arguments feature introduced in Java 1.5 */ public class NoVarArgs { public static void main(String[] args) { int arr[] = new int[]{50, 10, 5, 3, 2}; int num1 = 10; int num2 = 20; System.out.println(subVarArgsMethod(arr)); System.out.println(subVarArgsMethod(num1, num2)); } public static int subVarArgsMethod(int... a){ int answer = 0; for (int i: a) answer = answer - i; return answer; } }
or we could even use different data structures to do this. Wouldn’t it be nicer if Java has a feature to make it simpler in some way?. YES. Of course and that is where varargs feature introduced in Java 1.5 to helps us. Look at the below varargs example which passes the array of int values to a method.
/** * This class is to illustrate the basic need for var arguments feature introduced in Java 1.5 */ public class NoVarArgs { public static void main(String[] args) { int arr[] = new int[]{50, 10, 5, 3, 2}; int num1 = 10; int num2 = 20; System.out.println(subVarArgsMethod(arr)); System.out.println(subVarArgsMethod(num1, num2)); } public static int subVarArgsMethod(int... a){ int answer = 0; for (int i: a) answer = answer - i; return answer; } }
How does Java do it? Java just creates an array internally allowing us to iterate through the variable number of arguments. The developer need not do anything extra to pass the values. Simple, isn’t it?
Varargs Syntax Advantages
- To use variable args features we just need to have 3 ellipsis dots like public static int subVarArgsMethod(int… a) in your method definition and that’s it. we can start using Java variable Arguments features now.
- Notice the advantage that variable arguments features bring in. In our earlier programs, we could pass only either an array or fixed number of arguments. But using variable args feature, we can see that we could use the same method for both arrays and any number of valid arguments.
- Of course, the same concepts can be used for Strings also.
- You can have several parameters in front of these variable arguments also. But you need to take care that this is the last argument in method definition. Eg.. public static int subVarArgsMet (double a, String s,int… a)
That’s it about Java variable arguments feature. Pass on your comments/correction if any. Thanks for reading.
This article is originally published at Java tutorials – Lets jump into the ocean, re-posted here with authors permission and as part of the JBC program.