It is a new feature introduced from java 5. It is same as array but in case of variable arguments we are passing directly value were as in case of array we need to create manually an array object. The task we are doing of creating an array is done by compiler itself in case of VAR-ARGS.

Important points regrading VAR-ARGS

1> Its simply reduced the task of creating an array manually, compiler will internally create the array of the specified type , only we need to pass the direct value in case of parameters passing.

2> The above we can do with the help of array as well but we need to create an array object and pass the array reference to the overloaded method or method call.

3> In case of array we cannot provide the actual value directly always, we need to provide the array object. But in case of VAR-ARGS we can directly pass value, compiler will internally convert it into an array.

4> To define VAR-ARGS we need to use three dots ( . . . ) after the data type name followed by the variable name.

5> VAR-ARGS will be converted as an array object internally by the compiler itself , to verify you can decompiler the .class file with the help of any java decompiler.

6> VAR-ARGS can be used as an array to access the data and we can use both length property and index notation to access the member declared as Variable argument.

7> While invoking the method with parameter as an VAR-ARGS we can pass either the direct value or an array reference pointing an array object of specified type.

8> In the same program we cannot have overloaded method with parameter as an array object and VAR-ARGS object . For ex-

class prog {

void show ( int[ ]  arr ){ }   // int type array  
void show ( int...  arr ) { }  // int type VAR-ARGS 
}

In the above code , for the compiler both are having same parameter i.e of array so it create and ambiguity for the compiler that which method is to be overloaded.

9> In case of overloading if we are using VAR-ARG as a parameter then it should be the last argument declared in the overloaded method. For ex-

void show ( String s , boolean b , int... arr ) // Valid

void show ( int... arr , String s , boolean b ) // Invalid

An example to show how VAR-ARGS is working

public class prog {

void add(int... b){        // overloaded method with VAR-ARGS
for(int i=0;i<b.length;i++) {
System.out.println(b[i]);
   }
}
public static void main(String sush[]){

 prog p = new prog();
        p.add(10,20,30,40,50);   // passing direct value 

   }
}

In the above code add(VAR-ARGS ) is used where we are passing direct value and overloaded method is with variable argument as an parameter. Internally compiler convert the direct value into an array object. To make you sure i m giving the decompiled code by the java Decompiler of the above program compiled code.

public class prog{

  void add(int[] paramArrayOfInt) {
      for (int i = 0; i < paramArrayOfInt.length; i++)  {

      System.out.println(paramArrayOfInt[i]);
     }
   }

  public static void main(String[ ] paramArrayOfString) {

   prog localprog = new prog();
   localprog.add(new int[]{10,20,30,40,50}); //Creating new 
                                               array object
   }
}

Here you can see that a new array object is created by the compiler itself and passing that array object reference to the overloaded add() method. 

0 comments:

Post a Comment

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic