1

I saw examples where one puts an array parameter to a function like this :

function f(String... strings)
{

}

and I guess this is one way of saying "expect an indefinite number of strings".

But how is it different from String[] strings ?

When / why / how should I use it (the three dots notation) ?

mobilGelistirici
  • 449
  • 4
  • 7
  • 19

4 Answers4

5

As you already guessed, the ... notation (called varargs) defines one or more parameters of the given type. In your method you will get a String[] in both cases. The difference is on the caller side.

public void test1(String... params) {
  // do something with the String[] params
}

public void test2(String[] params) {
  // do something with the String[] params
}

public void caller() {
  test1("abc"); // valid call results in a String[1]
  test1("abc", "def", "ghi"); // valid call results in a String[3]
  test1(new String[] {"abc", "def", ghi"}); // valid call results in a String[3]

  test2("abc"); // invalid call results in compile error
  test2("abc", "def", "ghi"); // invalid call results in compile error
  test2(new String[] {"abc", "def", ghi"}); // valid call
}
tmandry
  • 375
  • 3
  • 13
2

The difference between them is the way you call the function. With String var args you can omit the array creation.

public static void main(String[] args) {
    callMe1(new String[] {"a", "b", "c"});
    callMe2("a", "b", "c");
    // You can also do this
    // callMe2(new String[] {"a", "b", "c"});
}

public static void callMe1(String[] args) {
    System.out.println(args.getClass() == String[].class);
    for (String s : args) {
        System.out.println(s);
    }
}
public static void callMe2(String... args) {
    System.out.println(args.getClass() == String[].class);
    for (String s : args) {
        System.out.println(s);
    }
}

Source

Community
  • 1
  • 1
Basbous
  • 3,927
  • 4
  • 34
  • 62
1

Let me try to answer your question one by one.

Firstly function f(String... strings) { } The (...) identifies a variable number of arguments which means multiple arguments. you can pass the n number of arguments. Please refer the below example,

static int sum(int ... numbers)
{
   int total = 0;
   for(int i = 0; i < numbers.length; i++)
      total += numbers[i];
   return total;
}

and you can call the function like sum(10,20,30,40,50,60);

Secondly String[] strings also similar to multiple arguments but in Java main function you can use only string array. Here you can pass the arguments through command line on run time.

class Sample{
     public static void main(String args[]){
        System.out.println(args.length);
     }
 }

java Sample test 10 20

Thirdly, When and where you should use means,

Suppose you want to pass the arguments through command line also run time you can use String[] string.

Suppose you want pass n number of arguments at any time from your program or manually then you can use (...) multiple arguments.

user1584844
  • 276
  • 4
  • 12
0

String... strings is as you said an indefinite number of Strings String[] expects an array

The difference of both is, that the array has a fixed size he is initialized with. If it is function f(String[] strings) it's just the expected parameter.

Michael Brenndoerfer
  • 3,483
  • 2
  • 39
  • 50