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);
}
}
}
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.
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.
There's no difference, but putting the brackets after the type ( String[] ) is the more common practice in 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.
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.
5. String[] args. It stores Java command-line arguments and is an array of type java.
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);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With