Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between String[] data and String... data in java [duplicate]

Tags:

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

}
like image 359
Madumal Jeewantha Avatar asked Mar 07 '20 15:03

Madumal Jeewantha


People also ask

What is the difference between String [] and String in Java?

String[] and String... are the same thing internally, i. e., an array of Strings. The difference is that when you use a varargs parameter ( String... ) you can call the method like: public void myMethod( String... foo ) { // do something // foo is an array (String[]) internally System.

What does String [] mean in Java?

String [] arguments is a java array of String objects. This means that the main function expects an array of Strings. This array of strings typically holds all command line parameter arguments passed in when the program is run from the command line.

What is the difference between String [] args and String args?

There's no difference, but putting the brackets after the type ( String[] ) is the more common practice in Java.

What type is String [] Java?

util. regex. String: It is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once created.

What is difference between String and String array?

The key difference between Array and String is that an Array is a data structure that holds a collection of elements having the same data types, while a String is a collection of characters. Let's find out some major differences between array and string.

What is the use of String []?

5. String[] args. It stores Java command-line arguments and is an array of type java.


2 Answers

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);
    }
}
like image 130
Arvind Kumar Avinash Avatar answered Sep 30 '22 17:09

Arvind Kumar Avinash


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

like image 37
Alcastic Avatar answered Sep 30 '22 19:09

Alcastic