Var Arg Methods

Var Arg Methods

In this tutorial, we are going to discuss about Var Arg Methods in Java. In Java, a variable-length argument (varargs) method allows you to pass a variable number of arguments to a method. This feature was introduced in Java 5.0 as part of the enhanced for loop feature.

Var Arg Methods
  • Until Java 1.4 version we can’t declare a method with variable number of arguments if there is a change in number of arguments compulsory we should go for new method it increases the length of the code and reduces the readability.
  • To overcome this problem SUN people introduces var arg methods in 1.5 version according to this we can declare a method which can take variable number of arguments such type of methods are called var arg methods. We can declare a var arguments method as follows
method1(int... x)
  • We can call this method by passing any number of int values including zero number.
method1(); // Fine
method1(10); // Fine
method1(10,20,30); // Fine
method1(10,20,30,40); // Fine 

E.g

package com.ashok.test;

public class Test{
   public static void method1(int... x) {
      System.out.println("Hai this is Ashok");
   }
   public static void main(String args[]){
      method1(); // Hai this is Ashok
      method1(10); // Hai this is Ashok
      method1(10,20,30); // Hai this is Ashok
      method1(10,20,30,40); // Hai this is Ashok
   }
}

Internally var arg parameter will be converted in to one dimensional array hence with in the var arg methods we can differentiate values by using index.

E.g

package com.ashok.test;

public class Test {
   public static void sum(int... x) {
      int sum = 0;
      for(int y : x) {
         sum = sum + y;
      }
      System.out.println("Sum is : "+sum);
   }
   public static void main(String args[]) {
      sum(); // Sum is 0
      sum(10,20);// Sum is 30
      sum(10,20,30);// Sum is 60
   }
}

Case 1: Which of the following are valid?

method(int... x); // Fine
method(int ...x); // Fine
method(int x...); // Error
method(int. x..); // Error
method(int. ..x); // Error

Case 2: We can mix var arg parameter with normal parameter also

method(int x, String... s); // Fine

Case 3: We can mix var arg parameter with normal parameter but rule is it should be last parameter

method(int... x, String s); // Error

Case 4: In any var arg method we can take only one var arg parameter

method(int... x, String... s); // Error

Case 5: In general var arg method will get least priority i.e., if no other method matched then only var arg method will get chance. This is similar to default case inside switch.

package com.ashok.test;

public class Test {
   public static void method() {
      System.out.println("It is general method");
   }
   public static void method(int... x) {
      System.out.println("It is var arg method");
   }
   public static void main(String args[]) {
      sum(); // It is general method
      sum(10,20);// It is var arg method
      sum(10,20,30);// It is var arg method
   }
}

Case 6: Inside a class we can’t declare var arg method and it’s corresponding one dimensional array method simultaneously otherwise we will get compilation error.

package com.ashok.test;

public class Test {
   public static void method(int[] a) {
      System.out.println("It is one dimensional array method");
   }
   public static void method(int... x) {
      System.out.println("It is var arg method");
   }
   public static void main(String args[]) {
      sum();
      sum(10,20);
      sum(10,20,30);
   }
}
C.E: Cannot declare both method(int[]) and method(int...) in Test
Var arg vs Single dimensional arrays

Case 1: When ever single dimensional array present we can replace with var arg parameter. 

E.g

m1(int[] x) --> m1(int.. x) // Fine
m1(String[] x) --> m1(String.. x) // Fine

Case 2: When ever var arg parameter present we can’t replace with single dimensional array.

E.g

m1(int.. x) --> m1(int[] x) // Error
Varargs Limitation

You can have only one varargs parameter in a method, and it must be the last parameter. This limitation is because Java does not support methods with multiple varargs parameters.

That’s all about Var arg methods in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!

Var Arg Methods
Scroll to top