Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

Should a method that finds no results return null or an empty array or collection? What does the empty-array choice cost and how do you avoid it?

A method that finds nothing should return an empty array or collection, not null, so callers can loop over the result without a null check; this is the long-standing guidance in Effective Java, and returning null is a classic source of NullPointerException. The only cost of the empty choice is a tiny allocation, and we avoid even that by returning one shared instance: a zero-length array has no elements to change, so a single static final constant is safe to share, and List.of() and Collections.emptyList() already return shared immutable empty lists.

Why this question is asked

Whether to return null or an empty result looks like a matter of taste, but it decides how much defensive code every caller must write and how often the service fails with a NullPointerException in production. Interviewers ask it to hear the reasoning, not just the rule: why empty is better, what it really costs, and how the JDK itself avoids that cost. In this answer, we will see what goes wrong with null, why an empty result removes the problem, measure what an empty result costs, share a single instance to remove even that, and finish with the cases where null or Optional is still the right choice.

1What Goes Wrong When We Return null

Suppose a repository method returns null when a customer has no orders. Every caller must remember to check for null before using the result. The ones that remember add noise; the ones that forget work in testing, where customers usually have orders, and fail in production for the first new customer.

Java
Order[] findOrders(long customerId) {
List<Order> found = query(customerId);
return found.isEmpty() ? null : found.toArray(new Order[0]); // the problem
}
for (Order o : findOrders(42)) { ... } // NullPointerException for a customer with no orders
Order[] orders = findOrders(42);
if (orders != null) { for (Order o : orders) { ... } } // every caller repeats this

The deeper problem is that null carries no information the empty result would not carry. No orders and an empty array of orders mean the same thing, so null adds a special case without adding meaning.

2Return an Empty Result Instead

If the method returns an empty array or collection, every normal use just works: a for-each loop runs zero times, a stream has no elements, length is 0 and isEmpty() is true. The special case disappears from all the callers at once.

Java
List<Order> findOrders(long customerId) {
return query(customerId); // possibly empty, never null
}
for (Order o : findOrders(42)) { ... } // zero iterations for a new customer
findOrders(42).stream().mapToLong(Order::total).sum(); // 0
findOrders(42).isEmpty(); // true

This is the guidance Joshua Bloch gives in Effective Java: return empty collections or arrays, not nulls. It also makes the contract simpler to document, because the method never returns null, and tools such as nullness annotations can enforce that.

3What Does an Empty Result Cost?

An empty array is still an object. On a typical 64-bit HotSpot JVM, new Order[0] is about 16 bytes: the object header plus the length field, with no elements. Allocating it takes a few nanoseconds, because the JVM allocates from a thread-local buffer by bumping a pointer, and the garbage collector reclaims short-lived objects very cheaply. Often the JIT compiler removes the allocation entirely when the array does not escape the calling method.

So the cost is almost always irrelevant. It only matters on a very hot path, such as a method called millions of times per second that usually finds nothing. Even then, there is a simple way to make it zero.

4Sharing One Empty Instance

A zero-length array cannot be changed, because there is no element to assign. Its length is fixed at 0 and there are no indexes. That makes it effectively immutable, so one instance can safely be shared by every caller and every thread.

Java
private static final Order[] NO_ORDERS = new Order[0];
Order[] findOrders(long customerId) {
List<Order> found = query(customerId);
return found.isEmpty() ? NO_ORDERS : found.toArray(NO_ORDERS);
}
List.of() == List.of(); // true: one shared immutable instance
Collections.emptyList() == Collections.emptyList(); // true
List.of().add(new Order()); // UnsupportedOperationException
What a method can return when it finds nothing
Return valueCaller codeCost
nullMust check before every useNone, but NullPointerException risk
new Order[0] each timeLoops and streams just workAbout 16 bytes, often optimised away
Shared static final empty arrayLoops and streams just workNothing
List.of() or Collections.emptyList()Works; cannot be modifiedNothing: shared singleton
The shared empty instance gives the safety of an empty result at no cost.

For collections, the JDK already does this for us: List.of(), Set.of(), Map.of() and Collections.emptyList() return shared immutable singletons, so returning them allocates nothing. Notice the last line, which is the one thing callers must know: the shared empty list cannot be modified. That is a good contract anyway, because callers should not modify a result they did not create; if they need a mutable copy, they make one with new ArrayList<>(result).

5The toArray Idiom

Converting a collection to an array is where empty arrays appear most often. There are three common forms. Modern HotSpot JVMs handle the zero-length form at least as fast as the pre-sized one, because the JVM can allocate and fill the right-sized array in one step without first clearing it, so the pre-sized form is no longer an optimisation.

Java
String[] a = names.toArray(new String[0]); // the usual idiom
String[] b = names.toArray(String[]::new); // Java 11: same result, reads clearly
String[] c = names.toArray(new String[names.size()]); // older advice; no faster today

6When null or Optional Is Still Right

The rule is about results that are collections. A method that returns one value which may be absent, such as findById, has no empty form, and there Optional<Order> is the standard way to make absence explicit in the type. We should not wrap collections in Optional, as in Optional<List<Order>>, because the empty list already expresses no results and the wrapper only adds a second kind of nothing.

Null can still be right inside an implementation, for example a lazily initialised field, or where null genuinely means something different from empty, such as not loaded yet versus loaded and empty. Even then it is better to keep that null private and give callers an explicit type or method, such as isLoaded(), instead of a null they must interpret.

7Follow-up questions

Should a method return Optional<List<T>> to show there might be no results?

No. An empty list already means no results, so Optional<List<T>> gives callers two ways to say nothing, an empty Optional and an Optional containing an empty list, and they must handle both. Optional is meant for a single value that may be absent, such as Optional<Customer> findById(id). For collections, return an empty collection.

Is Collections.emptyList() the same as List.of()?

Both return a shared immutable empty list, so in most code they are interchangeable. There are small differences: List.of() belongs to the newer immutable collections that reject null, so List.of().contains(null) throws NullPointerException, while Collections.emptyList().contains(null) returns false. In modern code, List.of() is the usual choice.

What if a caller wants to modify the returned list?

Then the caller should copy it, with new ArrayList<>(result). A method that returns its internal list, or a list a caller then changes, creates hidden coupling between the two, so returning an unmodifiable result is the safer contract even when it is not empty. If a method's purpose is to return a list for the caller to fill, it should document that and return a new mutable list every time, never a shared empty instance.

8Conclusion

In this answer, we saw that returning null for no results forces every caller to check and causes NullPointerException when one forgets, while an empty array or collection makes loops, streams and size checks work unchanged, as Effective Java recommends. An empty array costs only a tiny, often optimised-away allocation, and because a zero-length array cannot change, we can return one shared static final instance; List.of() and Collections.emptyList() already share theirs. Optional remains the right tool for a single value that may be absent, never as a wrapper around a collection.

What to say in the interviewAbout 90 seconds aloud

Open with

I return an empty array or collection, never null. The reason is the callers: with an empty result, a for-each loop just runs zero times, streams work and size is zero, so no caller needs a null check, while returning null means every caller must remember to check, and the one that forgets fails in production with a NullPointerException, usually for the first customer with no data.

Then, if they want more

Null also carries no extra meaning, since no results and an empty list mean the same thing. This is the standard Effective Java advice. The cost of the empty choice is tiny: an empty array is about 16 bytes, allocation is a pointer bump, and the JIT often removes it.

Where it matters, on a very hot path, I avoid it completely: a zero-length array has no elements, so it's effectively immutable and I can return a single private static final instance to everyone.

For collections the JDK already does that, List.of and Collections.emptyList return shared immutable singletons, so callers just need to know they can't modify the result and should copy it if they need to. For toArray, I use new String[0] or the String array constructor reference, which is as fast as pre-sizing on modern JVMs.

The exception is a single value that may be absent, like findById, where I'd return Optional; but I wouldn't wrap a collection in Optional, because an empty list already says there's nothing.

About this answer

Last updated
12 November 2026
Versions it names
Java 11
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Should a method that finds no results return null or an empty array or collection? What...": https://scrivio-interview-library.netlify.app/java/arrays/3dace2a7d5/