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;
}