Var-arg methods are generally the special methods that can take variable number of arguments during its calling. Such methods finds its application in programming languages to decrease the length of code and increase its re-usability. For example:
class Vararg { public static add(int x, int y) //defining a method to add 2 numbers { System.out.println("Sum is "+(x+y)); //printing the sum of 2 members } public static void main(String [] args) { add(5,6); //calling the method } }
If in this program we want to add more than 2 numbers then we have to create a separate method for it. Or we can use 1 single method with variable number of arguments which can calculate the sum of any number of arguments. If we want to create a method with variable argument then the following syntax is :
methodname (datatype... variablename) //datatype is followed by exactly 3 dots '.' and then variable name { //method body }
Declaring the method internally with variable argument JVM understands that any number of arguments can be pass to it. So to process them an array is made of all the arguments that are fetch to the array. This array has the same name as the variable name in argument. Now consider the better version of the first code,
class Vararg { public static add(int... x) //defining a method to add 2 numbers { int sum = 0; System.out.prinrln("Number of variables passed is "+x.length); //Using the length property of array to determine the number of variables for(int i:x) { sum = sum+x[i]; } System.out.prinrln("Sum of variables passed is "+sum); //printing the sum of variables } public static void main(String [] args) { add(5,6); //calling the method with 2 arguments add(5,6,7); //calling the method with 3 arguments add(5,6,7,8); //calling the method with 4 arguments } }
Output:
Number of variables passed is 2 Sum of variables passed is 11 Number of variables passed is 3 Sum of variables passed is 18 Number of variables passed is 4 Sum of variables passed is 26
Possible Declarations for Var-Arg Methods
Case-1: These are some of the possible cases for syntax in arguments.
add(int... a) //valid add(int a...) //invalid add(int ...a) //valid add(int. ..a) //invalid add(int .a..) //invalid
Case-2: Defining a method with more than 1 argument and a variable argument.
student(String name, double... marks) //valid //a method that accepts a student name and marks in various subjects student(double... marks, String name) //invalid //variable argument should always be the last argument in method definition
Case-3: A method with more than 1 var-args parameter is not possible.
students(String... name, double... marks) //invalid
Case-4: If there are multiple methods with same name i.e. the method is overloaded then the priority of the var-args method will be least. It means that calling this method can only occur when no other method matches the criteria for method calling.
class VarArg { public static void add(int a, int b) //simple method { System.out.println("Simple method called."); } public static void add(int... a) //var arg method { System.out.println("Var-Arg method called."); } public static void main(String[] args) { //method calling add(5,6); add(5,6,7); } }
Output:
Simple method called. Var-Arg method called.
Case-5: A method cannot be overload in such a way that it is a var-args method and a method with single dimensional array as an argument.
class Student { public static void totalmarks(int[] a) //method with array as argument { //method body } public static void totalmarks(int... a) //var arg method { //method body } public static void main(String[] args) { //method calling add(5,6); add(5,6,7); } }
Note- While compiling this program we will get compile time error because method with var-args parameter and array parameter cannot be declare in a program lifecycles.
Var-arg and Single-Dimensional Arrays
In certain conditions, one may think that what is the difference between using var-args argument and single-dimensional array as both can take a variable number of arguments. Consider the following cases to understand the difference.
Case-1: If a method has a single-dimensional array as an argument then it can replace by a var-arg argument.
add(int[] a) => add(int... a) //valid add(double[] a) => add(double... a) //valid
Case-2: If a method has a var-arg argument then it cannot replace with an array argument.
add(int... a) => add(int[] a) //invalid add(double... a) => add(double[] a) //invalid
This is all about the Var-Arg Methods in Java.