The ArrayList.isEmpty() method returns true if the list contains zero elements (size() == 0), and false if at least one element is present.
Related List Operations: Adding an element with ArrayList.add() transitions isEmpty() from true to false, while emptying the list with ArrayList.clear() resets isEmpty() back to true. Always check !list.isEmpty() before calling list.get(0) or list.remove(0).
Declared in java.util.ArrayList<E> (implementing Collection.isEmpty()):
// Returns true if this list contains no elements (size == 0)
public boolean isEmpty()
| Aspect | Specification | Operational Rule |
|---|---|---|
| Return Value | boolean |
true if this list contains no elements; false otherwise. |
| OpenJDK Source | return size == 0; |
Directly compares the internal int size field against 0. |
| Time Complexity | O(1) |
Instant constant-time check regardless of backing array capacity. |
A newly constructed ArrayList has zero elements, so isEmpty() returns true. As soon as elements are added via add(), the list is no longer empty and isEmpty() returns false:
import java.util.ArrayList;
ArrayList<String> languages = new ArrayList<String>();
System.out.println("Before add: " + languages.isEmpty()); // true
languages.add("Java");
languages.add("PHP");
languages.add("Python");
System.out.println("After add: " + languages.isEmpty()); // false
ArrayList<Integer> marks = new ArrayList<Integer>();
System.out.println("Marks empty? " + marks.isEmpty()); // true
marks.add(55);
marks.add(54);
System.out.println("Marks empty? " + marks.isEmpty()); // falseTerminal Output:
Before add: true
After add: false
Marks empty? true
Marks empty? false
No! If you call list.add(null), the list now holds 1 element (whose value happens to be a null reference). Consequently, size() is 1 and isEmpty() returns false:
ArrayList<String> listWithNull = new ArrayList<>();
listWithNull.add(null); // 1 element added (whose value is null)
System.out.println("size(): " + listWithNull.size()); // 1
System.out.println("isEmpty(): " + listWithNull.isEmpty()); // false!Terminal Output:
size(): 1
isEmpty(): false!
isEmpty() only inspects the logical element count, never the internal array capacity. Pre-allocating capacity with new ArrayList<>(100) or ensureCapacity(100) still leaves isEmpty() as true, and calling clear() resets a populated list back to isEmpty() == true:
ArrayList<String> buffer = new ArrayList<>(100); // Capacity 100, size 0
System.out.println("New with capacity 100 -> isEmpty(): " + buffer.isEmpty()); // true
buffer.add("Job-1");
System.out.println("After add('Job-1') -> isEmpty(): " + buffer.isEmpty()); // false
buffer.clear();
System.out.println("After buffer.clear() -> isEmpty(): " + buffer.isEmpty()); // trueTerminal Output:
New with capacity 100 -> isEmpty(): true
After add('Job-1') -> isEmpty(): false
After buffer.clear() -> isEmpty(): true
An uninitialized variable (ArrayList<String> list = null;) is not an empty list—it is a null pointer. Calling list.isEmpty() on a null variable throws a NullPointerException:
ArrayList<String> uninitialized = null;
try {
uninitialized.isEmpty(); // Crash: calling a method on null!
} catch (NullPointerException ex) {
System.out.println("Caught NullPointerException on null reference!");
}Terminal Output:
Caught NullPointerException on null reference!
Understanding the exact difference between common list state expressions:
| Expression | Safe on null Reference? | Meaning & Recommendation |
|---|---|---|
list.isEmpty() |
No (throws NPE) | Preferred idiom when list is known to be non-null. |
list.size() == 0 |
No (throws NPE) | Equivalent on ArrayList, but less expressive than isEmpty(). |
list == null || list.isEmpty() |
Yes (Short-circuits) | Standard defensive guard for external method parameters. |
Using short-circuit evaluation c == null || c.isEmpty() protects service methods against both null references and empty collections:
import java.util.Collection;
public class CollectionGuard {
public static boolean isNullOrEmpty(Collection<?> c) {
return c == null || c.isEmpty();
}
public static void main(String[] args) {
ArrayList<String> a = null;
ArrayList<String> b = new ArrayList<>();
System.out.println("null list: " + isNullOrEmpty(a));
System.out.println("empty list: " + isNullOrEmpty(b));
}
}Terminal Output:
null list: true
empty list: true
Pairing while (!stack.isEmpty()) with stack.remove(stack.size() - 1) processes and removes elements in O(1) time per item until the list is empty:
public class LifoStackDrain {
public static void main(String[] args) {
ArrayList<String> stack = new ArrayList<>(Arrays.asList("Step-1", "Step-2", "Step-3"));
while (!stack.isEmpty()) {
String top = stack.remove(stack.size() - 1);
System.out.println("Processed: " + top + " | Remaining: " + stack.size());
}
}
}Terminal Output:
Processed: Step-3 | Remaining: 2
Processed: Step-2 | Remaining: 1
Processed: Step-1 | Remaining: 0
Writing if (list.isEmpty() || list == null) evaluates list.isEmpty() first and crashes with a NullPointerException when list is null! Always place list == null on the left side of || (or list != null on the left side of &&).
An ArrayList<String> containing [""] (an empty string) has 1 element, so list.isEmpty() is false, even though list.get(0).isEmpty() is true.
Compile and run this verification class in any Java environment:
public class ArrayListIsEmptyFullDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
if (list.isEmpty() && (list.size() == 0)) {
System.out.println("Check 1 Passed: New list isEmpty() is true.");
}
list.add("Item");
if (!list.isEmpty()) {
System.out.println("Check 2 Passed: Non-empty list returns false.");
}
list.clear();
if (list.isEmpty()) {
System.out.println("Check 3 Passed: Cleared list isEmpty() is true.");
}
System.out.println("All ArrayList.isEmpty() verification checks passed!");
}
}Terminal Output:
Check 1 Passed: New list isEmpty() is true.
Check 2 Passed: Non-empty list returns false.
Check 3 Passed: Cleared list isEmpty() is true.
All ArrayList.isEmpty() verification checks passed!
list.isEmpty() expresses intent directly ("is the list empty?") and is guaranteed to be O(1) across almost all Java collections, whereas size() on certain concurrent collections (like ConcurrentLinkedQueue) takes O(n) time.
No. In standard Java, negate isEmpty() using the logical NOT operator: if (!list.isEmpty()).
No. If list.isEmpty() is true, a for (String s : list) loop simply executes zero iterations without any error. Only guard with !list.isEmpty() when you execute extra setup or header logic outside the loop.
Effective Java Item 54 recommends returning an empty list (such as Collections.emptyList() or an empty ArrayList) rather than null so callers never suffer a NullPointerException.
isEmpty() becomes true.ArrayList.isEmpty() == true.ArrayList.ArrayList methods.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.