73

I'm trying to think of the best way to return an empty array, rather than null.

Is there any difference between foo() and bar()?

private static File[] foo() {
    return Collections.emptyList().toArray(new File[0]);
}
private static File[] bar() {
    return new File[0];
}
6
  • 2
    foo() is advised only to hide what the code is doing. Commented Jul 21, 2011 at 19:02
  • @Carlos, I admit it's convoluted, but the two methods are essentially equivalent, right? Commented Jul 21, 2011 at 19:03
  • 8
    equivalent if you ignore readability... you can complicate almost everything. What is the best way to return zero? return 15 - 3*5 + 0;? Commented Jul 21, 2011 at 19:08
  • @Carlos, Point taken! haha... Commented Jul 21, 2011 at 19:08
  • 3
    just a note: foo() will return the array created as the parameter passed to toArray! Commented Jul 21, 2011 at 19:17

8 Answers 8

90

A different way to return an empty array is to use a constant as all empty arrays of a given type are the same.

private static final File[] NO_FILES = {};
private static File[] bar(){
    return NO_FILES;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yeah, that's the other way I've seen it done. I think the fact that foo() worked disoriented me a bit. And I wasn't sure if referring to a constant would have any benefit. I don't know...just my inexperience, I suppose.
+1: Since an empty array is immutable, this is the best solution.
@Moonbeam, From a performance point of view it means you don't need to create an object each time.
@Peter, That being said, would foo() be more preferable than bar(), where the solution you provided would be optimal?
@Moonbeam, It still think foo() is doing more than it needs to, which can lead to confusion about what it is doing. It is often the case that people can assume a) extra was put there for a reason and it takes along time to find something which is not there, b) it is doing more work than it needs to, even if it doesn't)
|
45

Both foo() and bar() may generate warnings in some IDEs. For example, IntelliJ IDEA will generate a Allocation of zero-length array warning.

An alternative approach is to use Apache Commons Lang 3 ArrayUtils.toArray() function with empty arguments:

public File[] bazz() {
    return ArrayUtils.toArray();
}

This approach is both performance and IDE friendly, yet requires a 3rd party dependency. However, if you already have commons-lang3 in your classpath, you could even use statically-defined empty arrays for primitive types:

public String[] bazz() {
    return ArrayUtils.EMPTY_STRING_ARRAY;
}

1 Comment

Your IDE is just not smart enough to detect memory allocation in this case. ArrayUtils.toArray() creates a new File[0] every time it is called, which can be easily checked with comparing hash codes.
18

Definitely the second one. In the first one, you use a constant empty List<?> and then convert it to a File[], which requires to create an empty File[0] array. And that is what you do in the second one in one single step.

2 Comments

+1: The first one is confusing. But it doesn't create a list. The emptyList() returns a constant.
@Peter: Thanks, I didn't know. I edited my answer. I'll give you +1.
9

You can return empty array by following two ways:

If you want to return array of int then

  1. Using {}:

    int arr[] = {};
    return arr;
    
  2. Using new int[0]:

    int arr[] = new int[0];
    return arr;
    

Same way you can return array for other datatypes as well.

1 Comment

Or a combination of both: return new int[] {}
5
return new File[0];

This is better and efficient approach.

2 Comments

This doesn't answer the question at all.
A good answer would explain why it is more efficient, so readers can validate that it is correct and - more importantly - applicable for their situation. (And a bad comment alleges deficits that may or may not be true, as you say yourself in "I guess not" - that's just uncivil; such comments tend to get flagged and ultimatedly removed by a moderator.)
3

There is no difference except the fact that foo performs 3 visible method calls to return empty array that is anyway created while bar() just creates this array and returns it.

Comments

3

I'm pretty sure you should go with bar(); because with foo(); it creates a List (for nothing) since you create a new File[0] in the end anyway, so why not go with directly returning it!

Comments

3

In a single line you could do:

private static File[] bar(){
    return new File[]{};
}

1 Comment

new File[0] would to the same thing as new File[]{}, with less symbols, and I'd expect it to compile to the same bytecode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.