Variable Argument (Varargs):

Variable Argument (Varargs):

The varrags allows the method to accept zero or muliple arguments. Before varargs either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem. If we don’t know how many argument we will have to pass in the method, varargs is the better approach.

Advantage of Varargs:

We don’t have to provide overloaded methods so less code.


Syntax of varargs:

The varargs uses ellipsis i.e. three dots after the data type. Syntax is as follows:

  1. return_type method_name(data_type… variableName){}

 

Simple Example of Varargs in java:

  1. class VarargsExample1{
  2.  static void display(String… values){
  3.   System.out.println(“display method invoked “);
  4.  }
  5.  public static void main(String args[]){
  6.  display();//zero argument 
  7.  display(“my”,“name”,“is”,“varargs”);//four arguments
  8.  }
  9. }

Test it Now

Output:display method invoked
       display method invoked

Another Program of Varargs in java:

  1. class VarargsExample2{
  2.  static void display(String… values){
  3.   System.out.println(“display method invoked “);
  4.   for(String s:values){
  5.    System.out.println(s);
  6.   }
  7.  }
  8.  public static void main(String args[]){
  9.  display();//zero argument 
  10.  display(“hello”);//one argument 
  11.  display(“my”,“name”,“is”,“varargs”);//four arguments
  12.  }
  13. }

Test it Now

Output:display method invoked
       display method invoked
       hello
       display method invoked
       my
       name
       is 
       varargs
download this example

 


Rules for varargs:

While using the varargs, you must follow some rules otherwise program code won’t compile. The rules are as follows:

  • There can be only one variable argument in the method.
  • Variable argument (varargs) must be the last argument.

Examples of varargs that fails to compile:

  1. void method(String… a, int… b){}//Compile time error
  2. void method(int… a, String b){}//Compile time error

Example of Varargs that is the last argument in the method:

  1. class VarargsExample3{
  2.  static void display(int num, String… values){
  3.   System.out.println(“number is “+num);
  4.   for(String s:values){
  5.    System.out.println(s);
  6.   }
  7.  }
  8.  public static void main(String args[]){
  9.  display(500,“hello”);//one argument 
  10.  display(1000,“my”,“name”,“is”,“varargs”);//four arguments
  11.  }
  12. }

Test it Now

Output:number is 500
       hello
       number is 1000
       my
       name
       is 
       varargs
download this example
Next Topicstatic-import-in-java

 

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Variable Argument (Varargs): The varrags allows the method to accept zero or muliple arguments. Before varargs either we use overloaded method or take an array as the method parameter but it was not considered good because it leads to the maintenance problem. If we don’t know how many argument we will have to pass in […]

Tags:

....
...