|
In past releases of Java, if user want to send the an arbitrary number of values then there are two ways either to create an array and put the values into it or create some overloaded methods for the same. In 5.0, Sun has introduced variable argument concept which automatically take the array of values passed to the method. The syntax of varargs is as follows :-
public returntype methodname(Object... arguments);
The following example will demonstrate the use of Variable Arguments in Java5.0 .
package com.visualbuilder;
public class VarargsExample {
public static void getArgument(String... arguments){ for (int i=0;i<arguments.length ; i++){ System.out.println(arguments[i]); } } public static void main(String[] args) { getArgument("hello","Visual","Builder"); getArgument("hello","Visual"); }
}
Output:-
The following output will be displayed on console.
hello
Visual
Builder
hello
Visual |