Showing posts with label Java varargs code sample. Show all posts
Showing posts with label Java varargs code sample. Show all posts

Wednesday, 8 October 2014

Java - Varargs

Varargs is a very good feature introduced in Java 1.5.  This feature allows programmers to pass variable number of argument of same type to a method.  Some may confuse it with overloading.  But it is actually different.  We will learn the differences between overloading and varargs below.

Difference between method overloading and varargs

Overloading.
1. Method signature may contain arguments of different data types.
Eg. public long add(int i,long l)



Varargs
1. varargs parameter in method signature will be of same data type.
    public long add(long l1, long ... numbers)

2. The varargs parameter should be last of the arguments to the method. If a method has only one parameter of varargs type, it is allowed.

Eg 1. Please see the above example.
Eg 2. Invalid code.
       public long add(long ... numbers, long l1)

Eg. 3 public long add(long ... numbers)


Code Sample:



public class VarargsDemo {
    
    /*
      The following method uses varargs parameter (numbers).
      Java considers varargs parameter as an array.
      So the variable numbers can be used as an array of integers.
    */

public int add(int n1)
{
System.out.println("Calling add method with 1 argument : ");
return n1;
}

public int add(int n1,int n2)
{
System.out.println("Calling add method with 2 arguments : ");
return n1 + n2;
}

public int add(int n1,int n2,int n3)
{
System.out.println("Calling add method with 3 arguments : ");
return n1 + n2 + n3;
}

    public int add(int n1,int ... numbers)
{
System.out.println("Calling add method varargs as argument : ");
int sum=n1;
for(int i=0;i<numbers.length;i++) {
sum = sum + numbers[i];
}
return sum;
}

public double average(double n1, double ... numbers)
{
double sum=n1;
for(int i=0;i<numbers.length;i++) {
sum = sum + numbers[i];
}
return sum/(numbers.length+1);
}

public static void main(String[] args)
{
VarargsDemo vd=new VarargsDemo();
// Sample usage of add Method
System.out.println("The sum of all numbers is : "+vd.add(1,2));
System.out.println("The sum of all numbers is : "+vd.add(1,2,3,4));
System.out.println("The sum of all numbers is : "+vd.add(1));
System.out.println("The sum of all numbers is : "+vd.add(10,11,12,1000,12,11,5000,12000));

// Sample usage of average method which uses varargs
System.out.println("The average of all numbers is : "+vd.average(1.25,2.75));
System.out.println("The average of all numbers is : "+vd.average(1.0,2.0,3.5,4.75));
System.out.println("The average of all numbers is : "+vd.average(1.0));
System.out.println("The average of all numbers is : "+vd.average(10.0,11.12,12.23,1000.34,12.45,11.56,5000.0,12000.0));
}
}


Output: 


Calling add method with 2 argument : 
The sum of all numbers is : 3
Calling add method varargs argument : 
The sum of all numbers is : 10
Calling add method with 1 argument : 
The sum of all numbers is : 1
Calling add method varargs argument : 
The sum of all numbers is : 18056
The average of all numbers is : 2.0
The average of all numbers is : 2.8125
The average of all numbers is : 1.0
The average of all numbers is : 2257.2125


Learning:

1. When we have two methods a. overloaded method and b. varargs method,  when we invoke a method with number of arguments which can fit into the overloaded method, then  the overloaded method will be called.  

Eg. In the above code, both the following methods can be used to invoke add() with 2 arguments.  If we invoke add(10,20), the method (a) will be called instead of method (b).

a. add(int n1,int n2) 
b. add(int n1,int ...numbers)