Friday, November 26, 2010

variable number of arguments

Question : What is Variable number arguments :
Ans : It is a new feature in java 1.5 [ also called as tiger ] 
Example : public static void mymethod(String ... objects)
                 {
                          for(String  s : myString)
                  {
                      Syso("String : "+s);
                 }
              }

Note : variable number of arguments type statements should be last argument  in the 
          method definition.


Example :
                 This code is correct :


      int n = 43000;
      int number = 500;
      String name = "sekhae23";
     String myName = "Karri,sekhar";
     myMethod(n,number,myName,name);


   public static void  myMethod(int x,int y,String ... objects){
       //Observer variable number of arguments argument is last ....
for(String s : objects){
   System.out.println(" S >>>> "+s);
}
System.out.println(" ");
    }
Below code snippet is Wrong :

     int n = 43000;
     String myName = "Karri,sekhar";
     String name = "sekhae23";
     int number = 500;
     myMethod(myName,name,n,number);

   public static void  myMethod(String ... objects,int x,int y){
       //Observer variable number of arguments argument is last ....
for(String s : objects){
    System.out.println(" S >>>> "+s);
}
System.out.println(" ");
    }



Observation :   One Method can have only one variable number of arguments argument.



No comments:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...