The [vararg]
attribute indicates that the method accepts a variable number of parameters. To do this, the last parameter must be a safe array of type VARIANT, which contains all the other parameters:
[vararg [, optional-attributes]] return-type function-name( [optional-param-attributes] param-list, SAFEARRAY(VARIANT) last-param-name);
The varargs syntax basically allows you to specify that there are possible parameters, right? They may or may not be there. This is the goal of three points. When you call a method, you can call it with or without these parameters. This was done to avoid having to pass arrays to methods.
Check this out :
See When do you use varargs in Java?
final public class Main { private void show(int []a) { for(int i=0;i<a.length;i++) { System.out.print(a[i]+"\t"); } } private void show(Object...a) { for(int i=0;i<a.length;i++) { System.out.print(a[i]+"\t"); } System.out.println("\nvarargs called"); } public static void main(String... args) { int[]temp=new int[]{1,2,3,4}; Main main=new Main(); main.show(temp); main.show();
For this reason, varargs
is generally not recommended when overloading methods.
System.out.printf();
is an example of varargs
and is defined as follows.
public PrintStream printf(String format, Object ... args) { return format(format, args); }
format - A format string, as described in the format string syntax
args - Arguments referenced by format specifiers in the format string. If there are more arguments than format specifiers, additional arguments are ignored. The number of arguments is a variable and can be zero. The maximum number of arguments is limited by the maximum size of the Java array, as defined by the Java Virtual Machine specification. The behavior on the null argument depends on the conversion.