2

I didn't know what to look for so i couldn't google this one but..

What are String... parameters called as ?

And how do you get individual strings when more than one String object is passed through a String... object ?

Raicky Derwent
  • 384
  • 1
  • 4
  • 14

2 Answers2

2

A String... is syntactic sugar for an String[] and you access the values of the String array just like any other String array. In fact you can write this simple method.

public static <T> T[] asArray(T... ts) {
    return ts;
}

This takes a vararg array can returns an array.

The key difference is that you can write

asArray(1, 2, 3); // much shorter.

instead of having to write

asArray(new Integer[] { 1, 2, 3 });
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

this is a feature introduced with jdk 1.5. basically the last parameter of a method can be declared as ... and that means

  • from method caller pespective you can pass zero, one or many instances of that type
  • within method the .... param is basically seen as an array
Andrea
  • 2,714
  • 3
  • 27
  • 38