Home Java ArrayList Methods isEmpty() Java ArrayList isEmpty() Method Definition In Java, isEmpty() returns true if an ArrayList holds no elements, and false otherwise. It means exactly the same as size() == 0, but reads more clearly.
Check it before reading an element that might not exist, such as get(0). Note that a list holding a single null is not empty, and a null variable is not an empty list: calling isEmpty() on it throws NullPointerException.
An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe .
Syntax No arguments; returns a boolean.
List<String> list = new ArrayList<>();
boolean e1 = list.isEmpty(); // true
list.add("x");
boolean e2 = list.isEmpty(); // false Call What it does list.isEmpty()true if the list has no elements.!list.isEmpty()true if there is at least one element.list == null || list.isEmpty()Null-safe check for a list that may be missing. list.size() == 0Same result, less direct.
isEmpty() never changes the list and takes constant time.
Library declarations // java.util.ArrayList<E>
public boolean isEmpty()
// Declared in Collection<E> and List<E> Parameters Parameter Description None isEmpty() takes no parameters.
Return Value Return Value Description booleantrue if the size is 0; otherwise false.
Important
isEmpty() looks only at the number of elements, not at their values: a list of empty strings or null values is not empty. Calling it on a null reference throws NullPointerException, so check for null first when the list may be missing.
Compare with Similar Methods Related checks and resets:
Method How it differs size()The exact count of elements. clear()Removes everything, so isEmpty() becomes true. getFirst()Throws on an empty list (Java 21+). contains(o)Checks for a specific value instead.
Java Examples 1. Empty or not 2. Guard a read 3. null vs empty Example 1 — Check a Cart Before and After Changes Watch isEmpty() as an item is added and the list is cleared.
import java.util.ArrayList;
import java.util.List;
public class ArrayListIsEmptyDemo {
public static void main(String[] args) {
List<String> cart = new ArrayList<>();
System.out.println("empty at start? " + cart.isEmpty());
cart.add("tea");
System.out.println("after add? " + cart.isEmpty());
cart.clear();
System.out.println("after clear? " + cart.isEmpty() + " (size " + cart.size() + ")");
}
} empty at start? true
after add? false
after clear? true (size 0) How It Works New list. A freshly created ArrayList holds nothing, so isEmpty() is true.One item. After add("tea") the size is 1, so the answer flips to false.Cleared. clear() removes every element, making the list empty again.Same as size 0. The size printed next to it confirms that isEmpty() is simply size() == 0.Example 2 — Guard get(0) with isEmpty() Return a default value when there is no first element.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListIsEmptyGuard {
public static void main(String[] args) {
List<String> winners = new ArrayList<>();
System.out.println("winner: " + firstOrDefault(winners, "no winner yet"));
winners.addAll(Arrays.asList("Tara", "Uma"));
System.out.println("winner: " + firstOrDefault(winners, "no winner yet"));
}
static String firstOrDefault(List<String> list, String fallback) {
return list.isEmpty() ? fallback : list.get(0);
}
} winner: no winner yet
winner: Tara How It Works Guard first. The helper checks isEmpty() before calling get(0), which would throw on an empty list.Fallback. With no winners yet, the helper returns the default text instead of failing.Data arrives. addAll adds Tara and Uma, so the list is no longer empty.First element. Now the helper returns get(0), which is Tara.Example 3 — A null List vs an Empty List Write a null-safe check and see that a list holding null is not empty.
import java.util.ArrayList;
import java.util.List;
public class ArrayListIsEmptyNull {
public static void main(String[] args) {
List<String> missing = null;
List<String> empty = new ArrayList<>();
List<String> holdsNull = new ArrayList<>();
holdsNull.add(null);
System.out.println("missing: " + isNullOrEmpty(missing));
System.out.println("empty: " + isNullOrEmpty(empty));
System.out.println("holdsNull: " + isNullOrEmpty(holdsNull) + ", size " + holdsNull.size());
}
static boolean isNullOrEmpty(List<?> list) {
return list == null || list.isEmpty();
}
} missing: true
empty: true
holdsNull: false, size 1 How It Works Short-circuit. list == null is checked first; when it is true, || skips isEmpty(), so no NullPointerException occurs.Empty list. A real list with no elements passes the null test and then reports isEmpty() as true.A null element counts. holdsNull contains one element whose value is null, so its size is 1 and it is not empty.Any element type. The List<?> parameter lets the helper accept a list of any element type.Frequently Asked Questions How do I check if an ArrayList is empty in Java? Call list.isEmpty(). It returns true when the list has no elements.
Is isEmpty() better than size() == 0? They give the same result. isEmpty() states the intent more clearly, so it is usually preferred.
Is a list containing null empty? No. A null element is still an element, so the size is 1 and isEmpty() returns false.
What happens if I call isEmpty() on a null list? You get a NullPointerException. Use list == null || list.isEmpty() when the list may be missing.
Does clear() make isEmpty() return true? Yes. After clear() the size is 0, so isEmpty() returns true.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion