{}
defines the contents of the array, in this case it is empty. These would both have an array of three String
s
String[] array = {"element1","element2","element3"};
String[] array = new String[] {"element1","element2","element3"};
while []
on the expression side (right side of =
) of a statement defines the size of an intended array, e.g. this would have an array of 10 locations to place String
s
String[] array = new String[10];
...But...
String array = new String[10]{}; //The line you mentioned above
Was wrong because you are defining an array of length 10 ([10]
), then defining an array of length 0 ({}
), and trying to set them to the same array reference (array
) in one statement. Both cannot be set.
Additionally
The array should be defined as an array of a given type at the start of the statement like String[] array
. String array = /* array value*/
is saying, set an array value to a String
, not to an array of String
s.