158

The Java Docs for the method
String[] java.io.File.list(FilenameFilter filter)
includes this in the returns description:

The array will be empty if the directory is empty or if no names were accepted by the filter.

How do I do a similar thing and initialize a String array (or any other array for that matter) to have a length 0?

2
  • 7
    I just realized this is a stupid question :( As these arrays are initialized exactly the same way as any other array just with a size 0. Shows how often I initialize arrays nowadays. I'll leave the question (not delete it) cause someday someone else might be just as stupid as I was just now :) Commented Nov 3, 2009 at 7:57
  • 1
    I am confused about what use case you would have for an array of length 0, and why you wouldn't simply init to null in that special case. Commented Mar 14, 2018 at 18:46

7 Answers 7

269

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.

Sign up to request clarification or add additional context in comments.

19 Comments

It seems that everybody likes typing: private static final String[] EMPTY_ARRAY = {};
@Thomas: I take your point, but for this particular case I prefer the more explicit form. It's clearer to me that it means "I want a string array with 0 elements" rather than "I want an array with this content - which is empty". Just personal preference I guess.
@Tony - I have to use the few places where Java can infer a type. :-)
@delive: The example I've provided will still create an empty array, but because it's empty, you can't use EMPTY_ARRAY[0] - that's trying to access element 0, which doesn't exist...
Looking at Apache Commons Lang, they have the following declaration in ArrayUtils: public static final String[] EMPTY_STRING_ARRAY = new String[0];
|
27

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

Comments

21
String[] str = {};

But

return {};

won't work as the type information is missing.

1 Comment

return new String[] { }; and return new String[0]; would both work.
16

Ok I actually found the answer but thought I would 'import' the question into SO anyway

String[] files = new String[0];
or
int[] files = new int[0];

3 Comments

Add such commentary to your question...or select one of the answers which said the same thing.
Thanks for the comment Jonathan. As you might have noticed I posted this answer before anyone else (and as such there were no answers to select). I also don't see how adding the answer to the question makes for a better question.
@Ron Tuffin -> asked Nov 3 '09 at 7:49:10Z -> answered Nov 3 '09 at 7:49:57Z It didn't even take you a minute to answer your own question? Really? :)
3

You can use ArrayUtils.EMPTY_STRING_ARRAY from org.apache.commons.lang3

import org.apache.commons.lang3.ArrayUtils;

    class Scratch {
        public static void main(String[] args) {
            String[] strings = ArrayUtils.EMPTY_STRING_ARRAY;
        }
    }

Comments

3

You can use the following things-

1. String[] str = new String[0];
2. String[] str = ArrayUtils.EMPTY_STRING_ARRAY;

Both are the same.

Comments

1

Make a function which will not return null instead return an empty array you can go through below code to understand.

    public static String[] getJavaFileNameList(File inputDir) {
    String[] files = inputDir.list(new FilenameFilter() {
        @Override
        public boolean accept(File current, String name) {
            return new File(current, name).isFile() && (name.endsWith("java"));
        }
    });

    return files == null ? new String[0] : files;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.