Java Stack empty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Stack method

Definition

In Java, empty() tests whether a Stack has no elements. It returns true when there is nothing to pop and false otherwise.

It is the standard loop condition for stack algorithms: while (!stack.empty()) keeps popping until the stack is drained. Stack also inherits isEmpty() from Vector, which always gives the same answer.

Declared in java.util.Stack itself (since Java 1.0). Stack<E> extends Vector<E> and keeps the top at the end of the vector, so printing a stack lists it from bottom to top.

Syntax

Call empty with no arguments; it only reads the size.

Java
Stack<Integer> stack = new Stack<>();
boolean a = stack.empty();   // true
stack.push(1);
boolean b = stack.empty();   // false
stack.pop();
boolean c = stack.empty();   // true again
CallWhat it does
stack.empty()true when the size is 0.
while (!stack.empty()) stack.pop()Pops everything, top first.
stack.isEmpty()Same result; declared in Collection and inherited from Vector.
stack.size() == 0Same result written by hand.

empty() runs in constant time: it compares the element count with zero.

Library declarations
Java
// java.util.Stack<E>
public boolean empty()

// body: return size() == 0;

Parameters

ParameterDescription
Noneempty() takes no parameters.

Return Value

Return ValueDescription
booleantrue if the stack contains no elements, false otherwise.

Compare with Similar Methods

Other ways to ask "is anything left?":

MethodHow it differs
isEmpty()Same answer; works on any Collection, so prefer it in code that accepts a List or Collection.
size()Tells you how many elements there are, not only whether there are any.
peek()Reads the top; throws EmptyStackException exactly when empty() would be true.

Java Examples

Example 1 — Work Through Chores Until None Are Left

Chores are pushed as they come up; the loop keeps going while empty() is false.

Java
import java.util.Stack;

public class StackEmptyDrain {
    public static void main(String[] args) {
        Stack<String> chores = new Stack<>();
        chores.push("wash");
        chores.push("dry");
        chores.push("fold");

        int done = 0;
        while (!chores.empty()) {
            System.out.println("doing: " + chores.pop());
            done++;
        }
        System.out.println(done + " chores done, empty = " + chores.empty());
    }
}

How It Works

  1. Loop on the condition. while (!chores.empty()) runs the body only while at least one chore is on the stack.
  2. Newest chore first. Each pop() takes the most recent chore, so they come out as fold, dry, wash, and done counts them.
  3. Stops at exactly zero. After the third pop empty() turns true, the loop ends, and pop() is never called on an empty stack.

Example 2 — Compare empty(), isEmpty(), and size()

Print all three after each change to a stack of plates, including a null element.

Java
import java.util.Stack;

public class StackEmptyVsIsEmpty {
    public static void main(String[] args) {
        Stack<String> plates = new Stack<>();
        System.out.println("new:      " + plates.empty() + " " + plates.isEmpty() + " " + plates.size());
        plates.push("plate");
        System.out.println("pushed:   " + plates.empty() + " " + plates.isEmpty() + " " + plates.size());
        plates.pop();
        System.out.println("popped:   " + plates.empty() + " " + plates.isEmpty() + " " + plates.size());
        plates.push(null);
        System.out.println("null top: " + plates.empty() + " " + plates.isEmpty() + " " + plates.size());
    }
}

How It Works

  1. Two names, one answer. In every state, Stack's own empty() and the inherited isEmpty() print the same value.
  2. Both follow the size. They are true exactly when size() is 0: before the push and after the pop.
  3. A null element still counts. After push(null) the stack holds one element, so neither method calls it empty.

Example 3 — Disable Undo When There Is Nothing to Undo

An undo() method checks empty() so extra clicks never crash the program.

Java
import java.util.Stack;

public class StackEmptyUndoGuard {
    static Stack<String> history = new Stack<>();

    static void undo() {
        if (history.empty()) {
            System.out.println("Nothing to undo");
        } else {
            System.out.println("Undid: " + history.pop());
        }
    }

    public static void main(String[] args) {
        history.push("draw circle");
        undo();
        undo();
        history.push("draw square");
        undo();
    }
}

How It Works

  1. Check before acting. undo() asks history.empty() first, so the pop only happens when there is an action to reverse.
  2. A second click is harmless. After "draw circle" is undone the history is empty, and the second call prints a message instead of throwing EmptyStackException.
  3. Works again after a push. Drawing a square refills the history, so the third call finds that action and pops it.

Frequently Asked Questions

true if the stack has no elements, false if it has at least one.
They return the same result. empty() is declared in Stack since Java 1.0; isEmpty() comes from Collection through Vector and also works on lists, sets, and queues.
Yes, unless you prefer to catch EmptyStackException. A while (!stack.empty()) loop is the usual way to pop everything.
No. null is an element like any other, so a stack holding one null has size 1 and empty() returns false.
No. ArrayDeque and the other collections use isEmpty(). Only Stack has the extra empty() method.

Next: peek()

Look at the top element without removing it.

peek() 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.

7 people found this page helpful