What is the difference, if any, between these two ways of creating an array
in Java?
Method 1:
String[] s = new String[];
Method 2:
String s[];
Thanks in advance.
What is the difference, if any, between these two ways of creating an array
in Java?
Method 1:
String[] s = new String[];
Method 2:
String s[];
Thanks in advance.
There are a few ways to interpret your question,
// this was missing a size. So we need to add one.
int x = 10;
String []s = new String[x];
String s[] = new String[x]; // This is the same as the previous line.
The other form type of question is this,
//These are two null arrays.
String s[];
String []s;
So you can put the brackets before (or after) the array name in Java. That is a convenience the compiler offers, they're equivalent.
String[] s = new String[length];
This creates a new array of length length
and initializes its values with nulls.
String s[];
This declared a new array variable, which has the value of null
. It needs to be initialized before it can be used, and because it hasn't been initialized, it can't be said to have a length.