Course contentsShow
← Java
Lesson 242 of 2,8706. ArraysFree lesson

Zero-Length Arrays

Create valid empty arrays with length 0: new int[0], useful for representing empty collections.

Zero-Length Arrays

What you'll learn: How to create valid empty arrays with zero elements, and why they're useful in Java programs.

What Is a Zero-Length Array?

A zero-length array is an array that exists and is fully initialized, but contains no elements. You create one by specifying 0 as the size when using the new keyword:

int[] emptyNumbers = new int[0];
String[] emptyWords = new String[0];

These arrays are perfectly valid Java objects—they're not null, they just don't hold any elements.

Why Would You Ever Need This?

Think of a zero-length array like an empty shopping bag. The bag exists and is ready to use, but it's just not carrying anything at the moment. This is different from having no bag at all (null).

Zero-length arrays are especially useful when a method must return an array but has nothing to return. Instead of returning null (which forces callers to check for null every time), you can return an empty array. This makes code safer and cleaner:

// Safer: returns empty array instead of null
public int[] getScoresAbove90() {
    // No scores above 90? Return an empty array
    return new int[0];
}

Checking the Length

You can verify an array is empty by checking its length property:

int[] empty = new int[0];
System.out.println(empty.length);  // prints: 0

The array exists, but accessing any index (even empty[0]) throws an ArrayIndexOutOfBoundsException because there are no valid indices.

Key Takeaway: Zero-length arrays (new int[0]) are valid, initialized arrays with no elements—perfect for representing empty collections without using null.