Raicky Derwent
Raicky Derwent

Reputation: 393

What are Strings... objects called as?

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 ?

Upvotes: 2

Views: 136

Answers (2)

Andrea
Andrea

Reputation: 2764

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

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533492

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 });

Upvotes: 2

Related Questions