Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

like image 695
Peter Avatar asked Jan 23 '11 11:01

Peter


2 Answers

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.

like image 150
Brian Agnew Avatar answered Nov 07 '22 14:11

Brian Agnew


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.

like image 45
Oliver Charlesworth Avatar answered Nov 07 '22 15:11

Oliver Charlesworth