71

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];
}
Ivar
  • 6,138
  • 12
  • 49
  • 61
Moonbeam
  • 2,283
  • 1
  • 15
  • 17

8 Answers8

87

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;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 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. – Moonbeam Jul 21 '11 at 18:18
  • +1: Since an empty array is immutable, this is the best solution. – Martijn Courteaux Jul 21 '11 at 18:20
  • @Moonbeam, From a performance point of view it means you don't need to create an object each time. – Peter Lawrey Jul 21 '11 at 18:22
  • @Peter, That being said, would `foo()` be more preferable than `bar()`, where the solution you provided would be optimal? – Moonbeam Jul 21 '11 at 18:23
  • 2
    @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) – Peter Lawrey Jul 21 '11 at 18:30
  • @Peter, Thanks for the elaboration! :) – Moonbeam Jul 21 '11 at 18:31
  • another way: `return byte[]{};` – Captain Fantastic Apr 20 '18 at 19:31
44

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;
}
weekens
  • 8,064
  • 6
  • 45
  • 62
  • This is what I was looking for. Thank you. – Bandham Manikanta Jun 01 '20 at 14:00
  • 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. – Fizz Areh Aug 09 '22 at 08:32
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.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
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.

5
return new File[0];

This is better and efficient approach.

Gourav Goutam
  • 81
  • 1
  • 4
  • This doesn't answer the question at all. – Ignatius Aug 27 '19 at 11:17
  • 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.) – toolforger Sep 01 '20 at 08:58
3

In a single line you could do:

private static File[] bar(){
    return new File[]{};
}
Marco
  • 1,377
  • 15
  • 18
  • `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. – toolforger Sep 01 '20 at 08:59
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.

AlexR
  • 114,158
  • 16
  • 130
  • 208
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!

dominicbri7
  • 2,479
  • 4
  • 23
  • 33