Tuesday, 29 November 2016

Facts about Variable Length Argument in Java

Variable Length Argument Rules in Java


                 Java included a feature that simplifies the creation of methods that require variable number of arguments. This is known as vararg. vararg is the short for of Variable Length Arguments.

5 Facts about Variable Length Argument Rules in Java


1. A variable-length argument is specified by three periods (…).

     Eg:

            static void TestVariableArgument(int ... v) {
            }

         This syntax tells the compiler that TestVariableArgument( ) can be called with zero or more arguments.

2. In the case of no arguments, the length of the array is zero.

3. A method can have “normal” parameters along with a variable-length parameter and the variable-length parameter must be the last parameter declared by the method.


4. Overloading a method that takes a variable-length argument is possible.

5. If a method call conflicts with two methods that implements variable argument or other leads to error. 

No comments :

Post a Comment