Var-Arguments

Var-Arguments:

  • It is also introduce in jdk-5.
  • Allow to pass 0 to infinite number of arguments using single parameter in a method.
  • Reduce maintenance problem, before this we have to mention that how many arguments have to pass in a method as argument during compilation but now it will automatically hold any number of arguments .
  • It is helpful when we don't how many arguments have to pass.

Syntax: 

   return_type method_name(data_type ... variable_name){
//statements
}

Example:


class DemoVaragrs
{
          public static void main(String st[]){
                   DemoVarargs dv=new DemoVarargs();
                   dv.show(12,23);
                   dv.show(1,2,3,4);
          dv.show(23,45,67,89,90);
          }
          void show(int ...a)
          {
                   for(int aa:a)
               System.out.println(aa);
          }
}



**********************************************************************************
Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mobile: 9953584548
Write us at: - Smruti@apextgi.com and pratap@apextgi.com

No comments:

Post a Comment