1.

Why do we use variable length arguments in Java?

Answer»

The variable length arguments come into picture when you do not initially know the number of arguments PASSED to the method.

The variable length arguments can be zero or more. An example is shown below:

public class Demo {  public static void Varargs(String... str)  {    System.out.println("Arguments... ");    for (String s : str)      System.out.println(s);    System.out.println("Count = " + str.length );  }  public static void main(String ARGS[])  {    Varargs("ARG1", "arg2");  } }

The above program displays the following output:

Arguments... arg1 arg2 Count = 2


Discussion

No Comment Found