Powered by Blogger.

Variable argument or varargs in Java

>> Thursday, November 6, 2014

The full form of var-args is variable length arguments and this one of the java 1.5 features.Also we have some important autoboxing, generics,for-each loop, static imports and some more. This feature is introduced to solve the problem in defining methods for taking '0' to 'n' number of arguments. These arguments should be of same types.  So, by using this feature we no need to define multiple overloaded methods for executing same logic with different no.of.values of same type.
java var-args explanation_JavabynataraJ
For example adding 0 to n number of values by taking them as arguments.
Till java 1.4 we must define overloaded methods to pass different list of arguments as shown below
package com.javabynataraj;
class Addition {
 public void add(){
  System.out.println(10+20);
 }
 public void add(int a){
  System.out.println(a+20);
 }
 public void add(int a,int b){
  System.out.println(a+b);
 }
}
public class Calculator {
 public static void main(String[] args) {
  Addition ad = new Addition();
  ad.add();
  ad.add(10);
  ad.add(10, 20);
  ad.add(10,20,30); //cannot find add(int,int,int)
 }
}
The problem in the above design , user can pass values maximum 2 arguments. If user want to pass more than 3 values, developer must add three arguments overloaded method in "Addition" class then it should recompile and then should deliver it to user.
So user must wait till this new patch is delivered. Due to this problem user may loose business. The same problem is repeated if user wants to pass more than three values.
Solution:
As per Java1.4, the solution for the above problem is, we should define method in array as parameter. But again one more problem is raised that is calculator program user must create array object with values to call add () as shown below. This leads to Coding Problem.
package com.javabynataraj;
class Addition {
 void add(int[] a){
  int n = a.length;
  if(n==0){
   System.out.println("zero");
  }else if(n==1){
   System.out.println(a[0]);
  }else{
   int sum=0;
   for(int i=0;i<n;i++){
    sum+=a[i];
   }
   System.out.println(sum);
  }
 }
}
public class Calculator {
 public static void main(String[] args) {
  Addition ad = new Addition();
  ad.add(new int[0]);
  ad.add(new int[]{10});
  ad.add(new int[]{10,20});
  ad.add(new int[]{10,20,30});
  ad.add(new int[]{10,20,30,40});
 }//this code compiles successfully but having some design issues
}
The problem in the above design coding problem, i.e; Calculator application developer cannot pass integer values directly as shown below.
  ad.add();
  ad.add(10);
  ad.add(10,20);
User should pass values as array object. If we observe in the above code we use same syntax to create array object only values are changed.
Solution:
To avoid array creation to calla array parameter method varargs feature is introduced. If we define method with ‘varargs’ type parameter we can pass 0 to n number of values directly without object creation but internally varargs is single-dimensional array object . So, compiler internally does below two changes.
  1. It converts varargs  parameter type of single dimensional array type. 
  2. It converts method arguments from primitive type to array object. So as a developer by using ‘varargs ‘ concept we can define single method to pass 0 to n number of arguments directly as their types. Plus a single-dimensional array of that varargs type.
Syntax to create varargs parameter method:
  • After the data type we must place three dots.
  • variable arguments in java 5_JavabynataraJ
    for example,
    class Addition {
     void add(int... a){
      int n = a.length;
      if(n==0){
       System.out.println("zero");
      }else if(n==1){
       System.out.println(a[0]);
      }else{
       int sum=0;
       for(int i=0;i<n;i++){
        sum+=a[i];
       }
       System.out.println(sum);
      }
     }
    }
    public class Calculator {
     public static void main(String[] args) {
      Addition ad = new Addition();
      ad.add(new int[0]);
      ad.add(new int[]{10});
      ad.add(new int[]{10,20});
      ad.add(new int[]{10,20,30});
      ad.add(new int[]{10,20,30,40});
      ad.add(new int[]{10,20,30,40,50});
     }//this code compiles successfully
    }
    Above program shows varargs version of previous application(Addition). As we can observe in the above program varargs is replaced with single dimensional array and its arguments are converted as single dimensional array object, we can conclude that varargs s syntax is introduced to pass primitive values directly to array parameter method.
    Interview Question:
    Q. Is varargs syntax introduced for compiler or JVM or both?
    Ans. Only for compiler, varargs syntax will not available for JVM because compiler replaces varargs (...) syntax with single dimensional array("[]").

     So, we can conclude that varargs syntax is a communication between developer and compiler . Whenever user passes primitive values directly to the method call, so compiler understands and convert them to array object.

    Reference  Books:

    Related Posts Plugin for WordPress, Blogger...
    © javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.