Take the 2-minute tour ×

What does this mean? I can't tell what the first method m parameter declaration means.

It refuses to pass in a String but allows a single-dimensional array String [] and a two-dimensional array String[][]. It does not allow [][][] or anything more.

Can someone clear this up?

class j{
    public static void m(String [] ...  a) {

    public static void main(String[] args) {
        String [][] s = {};


share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

public static void m(String [] ... a) means your function can accept any number of String[] as arguments, and within the function you will have an array of String[] (i.e. a String[][]) as the type for a. You may also directly pass a single String[][] instead of passing multiple String[].

for example...

String [] x = {"a", "aa", "aaa"};
String [] y = {"b", "bb", "bbb"};
String [] z = {"c", "cc", "ccc"};

//All of these are valid uses of m(...).

In each case, m will have a two-dimensional array of Strings named a.

Additionally, since a is a String[][], you may also pass in a single String[][] directly.

String[][] XYZ = {x,y,z};

//Also valid. Same behaviour as m(x,y,z);
share|improve this answer
Even m() is possible. Dont overlook this. –  ollo Jan 5 '13 at 21:16
add comment

There are two ways of supplying the variable number of arguments of type T. You can either directly list expressions that are assignment convertible to T, or you can supply a single T[].

For the specific case, that means you can either list expressions of type String[], or supply a single String[][].

share|improve this answer
add comment

It is only a bit of syntax sugar from the compiler.

That is:

ReturnType withVarArgs(ParamType... params) {}

is mostly equivalent to:

ReturnType noVarArgs(ParamType[] params) {}

with the following differences:

  • If you have variables a, b, c of type ParamType, you can call withVarArgs function with the syntax withVarArgs(a, b, c); you would have to create an intermediary array yourself to call the noVarArgs variant.
  • A variable arguments parameter can only specified as the last parameter;
  • As pointed out in Effective Java, the behavior of Object... and Object[] will differ when you pass any array of a subtype of Object (e.g., let us say you want to pass a String[]):
    • if it is used with Object..., the String[] array object will be wrapped in a 1-element object array;
    • if it is used with Object[], the String[] array object will be used directly because arrays in Java are covariant on their element type;
share|improve this answer
add comment

Your Answer


