How can I initialize a String array with length 0 in Java? How can I initialize a String array with length 0 in Java? arrays arrays

How can I initialize a String array with length 0 in Java?


As others have said,

new String[0]

will indeed create an empty array. However, there's one nice thing about arrays - their size can't change, so you can always use the same empty array reference. So in your code, you can use:

private static final String[] EMPTY_ARRAY = new String[0];

and then just return EMPTY_ARRAY each time you need it - there's no need to create a new object each time.


String[] str = new String[0];?


String[] str = {};

But

return {};

won't work as the type information is missing.