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

}
3
  • 2
    String[] ... isn’t the same as String[] Commented Mar 7, 2020 at 15:36
  • @SamiKuhmonen Then my question is, why both test methods are allowed at the same time? and why always calling to String ... method? Commented Mar 7, 2020 at 15:51
  • 1
    Is your question about String[]... data like in your code or about String[] data like in your title? Commented Mar 7, 2020 at 16:05

4 Answers 4

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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)

Comments

0

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.