What is … in a method signature

折月煮酒 提交于 2019-12-02 02:53:27

It's varargs and can only be used last in a parameter list. The last param can hold more than one object.

public class C { 

    int i;
    String[] s;

    public C(int i, String... s){
        this.i = i;
        this.s=s;
    }
}
new C(4,"a","b") // will be transformed to int and String[]

See how "a" and "b" has transformed into an array.

The feature is called Varargs

It allows you to supply a random number of arguments to a method.

That is the varargs syntax: http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

It's treated like a GraphData[] which can be build on the fly as extensible parameters. Arrays.asList() is another such example.

This notation synonym for

public void addGraphData(GraphData[] _graphData) {

}

Lion

... indicates varargs in Java. The [vararg] attribute specifies that the method takes a variable number of parameters. To accomplish this, the last parameter must be a safe array of VARIANT type that contains all the remaining parameters :

[vararg [, optional-attributes]] return-type function-name(
[optional-param-attributes] param-list,
SAFEARRAY(VARIANT) last-param-name);

The varargs syntax basically lets you specify that there are possible parameters, right? They can be there, or cannot be there. That's the purpose of the three dots. When you call the method, you can call it with or without those parameters. This was done to avoid having to pass arrays to the methods.

Have a look at this:

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)  //See also here.
    {
        int[]temp=new int[]{1,2,3,4};            

        Main main=new Main();
        main.show(temp);
        main.show();         //<-- This is possible.
    }
}

It's for this reason, varargs is basically not recommended in overloading of methods.


System.out.printf(); is an example of varargs and defined as follows.

public PrintStream printf(String format, Object ... args)
{
     return format(format, args);
}

format - A format string as described in Format string syntax

args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.

The ... indicates a variable length parameter list.

printStrings(String... s){
   //internally s is an array with the parameters
   for(int i = 0; i < s.length;++i)
       System.out.print(s[i]);
}

printStrings("A","B","C","D");
//this is done by the compiler
printStrings(new String[]{"A","B","C","D"});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!