Peter
Peter

Reputation: 113

String... <varname> whats does String... mean

Im trying to figure out some java code. I came across something I have not seen before in a method header

private static object [] methodName(NodeList nodes, String... Names)

Whats is the operator ...?

Thanks and sorry did some searches could not find it elsewhere

Upvotes: 7

Views: 2919

Answers (2)

Brian Agnew
Brian Agnew

Reputation: 272237

That's a varargs declaration.

It's saying that you can call that method with 0 or more String arguments as the final arguments. Instead of:

write(new String[]{"A","B","C"});

you can use

write("A", "B", "C");

So each string is a different argument. You can then iterate through them e.g.

public void write(String... records) {
   for (String record: records)
      System.out.println(record);
}

More examples here.

Upvotes: 9

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

The ... denotes "varargs", i.e. you can provide an arbitrary number of String arguments. See http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html.

Upvotes: 2

Related Questions