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. 

Related Posts:

  • How and Why to use main() method in java? In this post i will tell you something interesting about main() method in java. Like What is the use of main() method? How we are going to use main() method? Why we are using main() method in our java program? You will find… Read More
  • Program to print a simple text in java After installing java and setting the path for it now its turn to have some fun with codes. Now i am going to tell you how to print your text in java. Its a very simple program and basically for beginners. There are some im… Read More
  • Introduction to Java Like all other programming language it is also a well known programming language basically designed for web application . As we all know that c++ is an objected oriented language but not fully object oriented, an attempt mad… Read More
  • Setting CLASSPATH in Java and configure it in windows ! Setting PATH and CLASSPATH are two different task in Java. Let me make you more clear about PATH setting and CLASSPATH setting in Java. If you had missed your lectures on PATH setting then this post will make you clear vi… Read More
  • Compilation and Interpretation in java In this post i will tell you something about compilation which is a task of compiler and on other side interpretation which is the task of interpreter.  There are few queries related to this topic which i am going … Read More

0 comments:

Post a Comment

Blog Archive

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic