Java Stack pop() Method

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

Definition

In Java, pop() removes the top element of a Stack and returns it. The top is the element pushed most recently, so a series of pops hands elements back in the reverse of the order they were pushed.

Use pop when you are done with the top element, for example to go back one page or undo one action. If you only want to look at it, use peek() instead. Calling pop on an empty stack throws EmptyStackException.

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 pop with no arguments; it returns the removed top element.

Java
Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
String top = stack.pop();     // "b", stack is [a]
stack.pop();                  // "a", stack is []
stack.pop();                  // throws EmptyStackException
CallWhat it does
stack.pop()Removes and returns the top element.
while (!stack.empty()) stack.pop()Takes every element, top first.
stack.peek()Returns the top without removing it.
stack.remove(stack.size() - 1)The same removal through the List API; fails with an index exception when empty.

pop runs in constant time: it reads the last element of the internal array and lowers the size by one. The array itself does not shrink.

Library declarations
Java
// java.util.Stack<E>
public synchronized E pop()

// throws java.util.EmptyStackException if the stack is empty

Parameters

ParameterDescription
Nonepop() takes no parameters.

Return Value

Return ValueDescription
EThe element that was on top. It can be null if null was pushed.

Compare with Similar Methods

Methods that read or remove the top:

MethodHow it differs
peek()Returns the top but leaves it on the stack.
remove(int)Removes by index and returns the element; remove(size() - 1) is a pop through the List API.
removeElementAt(i)Removes at any index and returns nothing.
ArrayDeque.pop()Same idea on a deque; throws NoSuchElementException when empty.

Java Examples

Example 1 — Browser Back Button

Each visited page is pushed; every Back click pops the current page.

Java
import java.util.Stack;

public class StackPopBackButton {
    public static void main(String[] args) {
        Stack<String> history = new Stack<>();
        history.push("home");
        history.push("products");
        history.push("product/42");
        history.push("cart");

        System.out.println("Back from: " + history.pop());
        System.out.println("Back from: " + history.pop());
        System.out.println("Now on: " + history.peek());
        System.out.println("history: " + history);
    }
}

How It Works

  1. Visit pages in order. Each page the user opens is pushed, so the current page, cart, is on top.
  2. Back means pop. Each Back click pops the current page and returns it: first cart, then product/42.
  3. The previous page resurfaces. After two pops, peek() shows products, and the history keeps only the two oldest pages.

Example 2 — Reverse a String with pop()

Push every letter, then pop them all: the letters come back in reverse order.

Java
import java.util.Stack;

public class StackPopReverse {
    public static void main(String[] args) {
        String word = "STACK";
        Stack<Character> letters = new Stack<>();
        for (char c : word.toCharArray()) {
            letters.push(c);
        }

        StringBuilder reversed = new StringBuilder();
        while (!letters.empty()) {
            reversed.append(letters.pop());
        }
        System.out.println(word + " -> " + reversed);
        System.out.println("letters left: " + letters.size());
    }
}

How It Works

  1. Push every character. The loop pushes S, T, A, C, K; each char is autoboxed to a Character, and K ends on top.
  2. Pop until empty. pop() hands the letters back reversed, K, C, A, T, S, and append collects them into KCATS.
  3. Fully drained. The condition !letters.empty() stops the loop exactly when nothing is left, so size() is 0 and no exception is thrown.

Example 3 — Pop an Empty Stack Safely

See EmptyStackException once, then avoid it with a check.

Java
import java.util.EmptyStackException;
import java.util.Stack;

public class StackPopEmpty {
    public static void main(String[] args) {
        Stack<Integer> tokens = new Stack<>();
        tokens.push(7);

        System.out.println("popped " + tokens.pop());
        try {
            tokens.pop();
        } catch (EmptyStackException e) {
            System.out.println("second pop: " + e.getClass().getSimpleName());
        }
        Integer safe = tokens.empty() ? null : tokens.pop();
        System.out.println("safe pop: " + safe);
    }
}

How It Works

  1. One element, one pop. The stack holds only 7, so the first pop() returns 7 and leaves the stack empty.
  2. Popping nothing fails. The second pop() throws EmptyStackException; the catch block prints the class name because this exception has no message.
  3. Guarded pop. The conditional expression checks empty() first and falls back to null, so the third attempt never throws.

Frequently Asked Questions

The element that was on top, which is the element pushed most recently. That element is removed from the stack.
It throws java.util.EmptyStackException, an unchecked exception. Check empty() or isEmpty() first.
Both return the top element. pop() also removes it; peek() leaves the stack unchanged.
Loop with while (!stack.empty()) { stack.pop(); } to process each element, top first. To discard everything without processing it, call clear().
No. It removes the last element of the internal array, which takes constant time no matter how big the stack is.

Next: push()

Put an element on top of the stack and get it back.

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

9 people found this page helpful