Tuesday, April 7, 2009

What is "String..."?

I saw "String..." in a method signature like this. What exactly is "String..."?
public void aMethod(String... args) {
...
}

"..." here is actually sort of a shortcut to Array. So the code above is pretty much the same as
public void aMethod(String[] args) {
...
}

However, when calling aMethod, you can do
aMethod("s1", "s2, "s3");
instead of
aMethod(new String[]{"s1", "s2", "s3"});
That is convenient. This can be used for any types of Array, such as "Integer...".

No comments:

Post a Comment