Java Stack isEmpty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Inherited from Vector

Definition

In Java, isEmpty() returns true when a Stack holds no elements and false otherwise. It is declared in the Collection interface and inherited through Vector.

Stack also has its own empty(), which gives the same answer. isEmpty() is the name every Java collection understands, so use it whenever your code works with a List or Collection rather than a Stack.

An instance method inherited from java.util.Vector (since Java 1.0). Stack does not override it: index 0 is the bottom of the stack and index size() - 1 is the top.

Syntax

Call isEmpty with no arguments.

Java
Stack<String> stack = new Stack<>();
boolean a = stack.isEmpty();   // true
stack.push("x");
boolean b = stack.isEmpty();   // false
boolean c = stack.empty();     // false: the same answer, Stack-only name
CallWhat it does
stack.isEmpty()true when there are no elements.
!stack.isEmpty()true when there is something to pop or peek.
stack.empty()Stack's own name for the same check.
collection.isEmpty()Works for any List, Set, Queue, or Stack.

isEmpty is constant time.

Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized boolean isEmpty()

// declared in java.util.Collection<E>

Parameters

ParameterDescription
NoneisEmpty() takes no parameters.

Return Value

Return ValueDescription
booleantrue if size() is 0.

Compare with Similar Methods

Related checks:

MethodHow it differs
empty()The same answer; exists only on Stack.
size()The exact count.
clear()Makes isEmpty() true.
ArrayList.isEmpty()The same method on a list.

Java Examples

Example 1 — Find Unclosed Tags

Match opening and closing tag names; isEmpty() both guards the peek() and decides the final verdict.

Java
import java.util.Stack;

public class StackIsEmptyTags {
    static String check(String[] tokens) {
        Stack<String> open = new Stack<>();
        for (String t : tokens) {
            if (t.startsWith("/")) {
                if (open.isEmpty() || !open.peek().equals(t.substring(1))) {
                    return "mismatch at " + t;
                }
                open.pop();
            } else {
                open.push(t);
            }
        }
        return open.isEmpty() ? "all closed" : "unclosed: " + open;
    }

    public static void main(String[] args) {
        System.out.println(check(new String[] {"html", "body", "/body", "/html"}));
        System.out.println(check(new String[] {"p", "b", "/b"}));
        System.out.println(check(new String[] {"/div"}));
    }
}

How It Works

  1. Openers wait on the stack. Each tag name is pushed; a token starting with / must close the tag currently on top.
  2. isEmpty guards the peek. For /div nothing is open, so isEmpty() is true and the method reports a mismatch instead of letting peek() throw.
  3. Leftovers are reported. At the end, a non-empty stack means some tags were never closed; for the second input that is p, printed as [p].

Example 2 — One Helper for Every Collection

A method that accepts any Collection can call isEmpty() but not empty().

Java
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Stack;

public class StackIsEmptyCollection {
    static String describe(String name, Collection<?> items) {
        return name + (items.isEmpty() ? " has nothing to do" : " has " + items.size() + " item(s)");
    }

    public static void main(String[] args) {
        Stack<String> undo = new Stack<>();
        List<String> todo = new ArrayList<>();
        todo.add("write tests");

        System.out.println(describe("undo stack", undo));
        System.out.println(describe("todo list", todo));
        undo.push("rename file");
        System.out.println(describe("undo stack", undo));
    }
}

How It Works

  1. Program to Collection. describe accepts any Collection<?>, so it can only call methods every collection has; empty() is not one of them, isEmpty() is.
  2. Same call, different types. The Stack and the ArrayList both pass through the helper, which reports the empty stack and the one-item list.
  3. Updates immediately. After a push, the same stack is no longer empty, and the helper reports 1 item.

Example 3 — null Elements, clear(), and null Variables

Three situations that are easy to mix up: a stack holding null, a cleared stack, and no stack at all.

Java
import java.util.Stack;

public class StackIsEmptyNullElement {
    public static void main(String[] args) {
        Stack<String> slots = new Stack<>();
        slots.push(null);
        System.out.println("holding null, isEmpty: " + slots.isEmpty() + ", size " + slots.size());

        slots.push("ticket");
        slots.clear();
        System.out.println("after clear, isEmpty: " + slots.isEmpty() + ", empty: " + slots.empty());

        Stack<String> missing = null;
        System.out.println("safe check on null variable: " + (missing == null || missing.isEmpty()));
    }
}

How It Works

  1. null is still an element. A stack holding one null has size 1, so isEmpty() is false.
  2. clear() empties for real. After clear(), both isEmpty() and Stack's empty() are true.
  3. A null variable is different. missing refers to no stack at all; || short-circuits on missing == null, so isEmpty() is never called on it and no NullPointerException occurs.

Frequently Asked Questions

true if the stack has no elements, false if it has at least one.
Both give the same result. isEmpty() works on every collection, so it is the better habit; empty() exists only on Stack.
No. A null element counts, so size() is 1 and isEmpty() is false.
They do the same work: both read the element count, which takes constant time.
Not on a real stack. Calling any method through a variable that is null throws NullPointerException, so check the variable first if it may be null.

Next: lastElement()

Read the top element; throws NoSuchElementException if empty.

lastElement() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful