What is … in a method signature

2019-03-01 06:32发布

问题:

I have seen it first time ... in a method signature.

I tried to access a .class file. It has a method defined as below

public void addGraphData(GraphData... _graphData) {
}

And that GraphData is nothing but POJO with getters and setters. Why is the .class file displaying GraphData... _graphData instead of GraphData _graphData ??

回答1:

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.



回答2:

The feature is called Varargs

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



回答3:

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.



回答4:

This notation synonym for

public void addGraphData(GraphData[] _graphData) {

}



回答5:

... 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.



回答6:

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"});