Var - Args

I am Aspiring full stack java Developer .
To understand the var args concept , let us solve the following problem
Write a class that has a method called sum( ) which can add and return the sum of all the integers passed to it as argument .
The method should be able to accept 2 3 or 4 arguments
One way to solve this problem is Using method overloading
class MyMath {
public static int sum(int a, int b) {
return a + b;
}
public static int sum(int a, int b, int c) {
return a + b + c;
}
public static int sum(int a, int b, int c, int d) {
return a + b + c + d;
}
}
class UseMyMath {
public static void main(String args[]) {
System.out.println(MyMath.sum(10, 20));
System.out.println(MyMath.sum(10, 20, 30));
System.out.println(MyMath.sum(10, 20, 30, 40));
}
}
Output : 30 60 100
Problems With The Code
Programmer has to design multiple methods even though they are performing the same task
Code is not flexible , i.e. in future if we want to add 5 integers it will not work
We cannot pass array as argument to any of these methods
Solution ?
- To solve this problem , from JDK 5 Java has included a feature called var args which stands for variable length arguments
What Is Var Args
Var args is a way of creating methods
It allows a method to accept zero or more arguments of a specified type .
This feature is very useful when the number of arguments passed to a method can vary
Syntax Of Var Args
A variable length argument is specified by three dots (…) called as ellipses , in the method’s formal argument list .
For Example,
public static void fun(int arr ) { // method body }This syntax tells the compiler that fun( ) can be called with zero or more arguments .
We can even pass an array as argument to this method while calling it.
Example
class Sample {
static void fun(int... a) {
System.out.println("Number of arguments: " + a.length);
for (int x : a)
System.out.print(x + " ");
System.out.println();
}
}
class UseSample {
public static void main(String args[]) {
Sample.fun(100); // one parameter
Sample.fun(1, 2, 3, 4); // four parameters
Sample.fun(); // no parameter
Sample.fun(new int[]{10, 20, 30, 40, 50}); // array as parameter
}
}
Output
Number of arguments: 1
100
Number of arguments: 4
1 2 3 4
Number of arguments: 0
Number of arguments: 5
10 20 30 40 50
Internal Working OF Var Args
Consider the following method
public int sumNumber (int x, int nums ) { // method body }The syntax tells the Java compiler that the method can be called with varying number of arguments
Now when we invoke a method with variable arguments , java compiler matches the arguments from left to right
Once it reaches to the last varargs parameter, it creates an array of the remaining arguments and pass it to the method
Rules About Var Args
The ellipses used to denote var args always have to be before the argument name and together Predict which one is correct ?
public static int sum(int … arr)// correct!
public static int sum(int … arr)// correct!
public static int sum(int… arr)// correct!
public static int sum(int arr)// incorrect!
public static int sum(int .arr..)// incorrect!
public static int sum(int . .. arr )// incorrect!
public static int sum(int .. . arr)// incorrect!
We can combine any number type of arguments with var args , but var args should be the last argument
Predict which one is correct ?
public static void display(int n,int … arr)// correct!
public static void display(String str,int … arr )// correct!
public static void display(double z, int… arr,String str)// incorrect!
There can be only one var args in a method
Predict which one is correct ?
public static void display(int … arr)// correct!
public static void display(char ch,int … arr )// correct!
public static void display(double … arr , int …)// incorrect!
From Java 5 onwards , we can have main method with var args as shown below public
public static void main(String … args)Now , when we will run the program , java will internally call the main(String … args ) method in the same way as it called main(String[ ] args)


