5

I have tried with data and data1 variables. It's always calling to String ... data. So, what is the difference between String[] data and String... data in java.

public class ArrayTest {

    public static void main(String[] args) {

        ArrayTest arrayTest = new ArrayTest();
        // Option one
        String[] data = {"A", "B", "C"};
        // Option two
        String data1 = "A";
        arrayTest.test(data);


    }

    public void test(String[] ... data  ) {
        System.out.println("---From: String[] ... data---");

        for(String[] item: data) {

            for(String innerItem : item) {
                System.out.println(innerItem);
            }

        }
    }

    public void test(String ... data  ) {
        System.out.println("---From: String ... data---");
        for(String item: data) {
            System.out.println(item);
        }
    }

}

4 Answers4

2

In test(String... data) you are passing an array of strings and in test(String[]... data) you are passing an array of arrays of strings. Check the updated code for illustration:

public class ArrayTest {

    public static void main(String[] args) {

        ArrayTest arrayTest = new ArrayTest();
        // Option one
        String[] data = { "A", "B", "C" };
        // Option two
        arrayTest.test(data);

        String[] data2 = { "D", "E" };
        arrayTest.test(data, data2);
    }

    public void test(String[]... data) {
        System.out.println("---From: String[] ... data---");

        for (String[] item : data) {

            for (String innerItem : item) {
                System.out.println(innerItem);
            }

        }
    }

    public void test(String... data) {
        System.out.println("---From: String ... data---");
        for (String item : data) {
            System.out.println(item);
        }
    }

}

Output:

---From: String ... data---
A
B
C
---From: String[] ... data---
A
B
C
D
E

In the presence of both versions of method signatures, JVM chooses the closest fit and that is why it goes to test(String... data) in case of arrayTest.test(data) while it goes to test(String[]... data) in case of arrayTest.test(data, data2).

The program will still work if you remove the following definition but then JVM will be left with only one choice, which is to go to test(String[]... data) for both the calls.

public void test(String... data) {
    System.out.println("---From: String ... data---");
    for (String item : data) {
        System.out.println(item);
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
2

When using Varargs (T...) arguments are packed into an array which is passed to the method at run time. You have already answered your question with your own implementation:

For:

1) public void test(String[] ... data) -- data is packed as String[][]

2) public void test(String ... data) -- data is packed as String[]

I strongly recommend the book: Java generics and collections - By Maurice Naftalin

Alcastic
  • 374
  • 5
  • 15
0
method(String... s) // fine
method(int i , String... s) // fine
method(String... s,int i) // not fine , ...s should be last or only parameter
method(String[] s) // fine
method(int i , String[] s) // fine
method(String[] s,int i) // fine
method(String s[]) // fine
method(String s...) // not fine can't put dot's after variable name
method(int[] i , String[] s) //fine
method(int... i , String...) // not fine, only one var arg is allowed
method(String... s) // takes any number of comma separated string also the array of String
method(String[] s) //only takes array of String but not comma separated string

At last -> (String[]... s) is equivalant to (String[][] s)

Abhinav Chauhan
  • 1,304
  • 1
  • 7
  • 24
0

String ... will read a single sentence with multiple words.

String[] ... will read a paragraph of multiple sentences.

Gopinath
  • 4,066
  • 1
  • 14
  • 16