Java ArrayDeque pop() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayDeque

Definition

In Java, pop() removes the element on top of a stack and returns it. An ArrayDeque keeps the top at its head, so pop() is the same as removeFirst().

It pairs with push(e): the last element pushed is the first popped. If the stack is empty, pop() throws NoSuchElementException. (The legacy Stack class throws EmptyStackException instead.)

An instance method of java.util.ArrayDeque (Java 1.6+): a resizable-array deque with no capacity limit, no null elements, and no thread safety.

Syntax

Call pop with no arguments to remove the top.

Java
Deque<String> stack = new ArrayDeque<>();
stack.push("a");
stack.push("b");
String top = stack.pop();     // "b", stack = [a]
CallWhat it does
stack.pop()Removes and returns the top; throws if empty.
stack.removeFirst()Exactly the same operation.
stack.poll()Also removes the head, but returns null when empty.
stack.peek()Reads the top without removing it.

If you want a stack that returns null instead of throwing, use poll(), which removes from the same end. Many programs simply guard pop() with isEmpty().

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

// Declared in Deque<E>

Parameters

ParameterDescription
Nonepop() takes no parameters.

Return Value

Return ValueDescription
EThe element at the top of the stack (the head).

Throw or Return a Special Value?

Each end has a method that throws when it cannot work (such as on an empty deque) and one that returns null or false instead. The highlighted cell matches pop().

OperationHead: throwsHead: special valueTail: throwsTail: special value
InsertaddFirst()offerFirst()addLast()offerLast()
RemoveremoveFirst()pollFirst()removeLast()pollLast()
ExaminegetFirst()peekFirst()getLast()peekLast()

Queue and stack names: add() = addLast(), offer() = offerLast(), push() = addFirst(), pop() = removeFirst(), remove() = removeFirst(), poll() = pollFirst(), element() = getFirst(), peek() = peekFirst().

Java Examples

Example 1 — Pop Books off a Pile

Push three books, then pop two of them.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequePopDemo {
    public static void main(String[] args) {
        Deque<String> pile = new ArrayDeque<>();
        pile.push("math");
        pile.push("art");
        pile.push("history");

        System.out.println(pile.pop());
        System.out.println(pile.pop());
        System.out.println(pile);
    }
}

How It Works

  1. Build the pile. history is pushed last, so it is on top.
  2. First pop. Returns history, the book on top.
  3. Second pop. Returns art, which was underneath.
  4. Bottom book. Only math, the first book pushed, is left.

Example 2 — Evaluate a Postfix Expression

A classic stack job: numbers are pushed, and each operator pops two numbers and pushes the result.

Java
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequePostfix {
    public static void main(String[] args) {
        String[] tokens = "5 1 2 + 4 * + 3 -".split(" ");
        Deque<Integer> stack = new ArrayDeque<>();

        for (String t : tokens) {
            if (t.equals("+") || t.equals("-") || t.equals("*")) {
                int b = stack.pop();
                int a = stack.pop();
                if (t.equals("+")) {
                    stack.push(a + b);
                } else if (t.equals("-")) {
                    stack.push(a - b);
                } else {
                    stack.push(a * b);
                }
                System.out.println(t + " -> " + stack);
            } else {
                stack.push(Integer.parseInt(t));
            }
        }
        System.out.println("result = " + stack.pop());
    }
}

How It Works

  1. Push numbers. After 5, 1, and 2 the stack is [2, 1, 5], top first.
  2. Order matters. For each operator the first pop() is the right operand b and the second is the left operand a. That is important for -.
  3. Step by step. 1 + 2 = 3, then 3 * 4 = 12, then 5 + 12 = 17, then 17 - 3 = 14; each result is pushed back.
  4. The answer. One number remains, and the final pop() returns 14.

Example 3 — Guard pop() on an Empty Stack

Pop the only element, use isEmpty() as a guard, then see what an unguarded pop() does.

Java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.NoSuchElementException;

public class ArrayDequePopEmpty {
    public static void main(String[] args) {
        Deque<Integer> stack = new ArrayDeque<>();
        stack.push(1);
        System.out.println("popped " + stack.pop());

        System.out.println(stack.isEmpty() ? "nothing to pop" : "popped " + stack.pop());

        try {
            stack.pop();
        } catch (NoSuchElementException e) {
            System.out.println("pop(): " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. One element. The only value, 1, is popped and printed.
  2. Guarded pop. The conditional operator checks isEmpty() first; it is true, so pop() is not called and the message is printed.
  3. Unguarded pop. Calling pop() on the empty stack throws NoSuchElementException.
  4. Not EmptyStackException. Code ported from java.util.Stack must catch the new exception type.

Frequently Asked Questions

It removes and returns the element at the top of the stack, which is the head of the deque.
Yes. For ArrayDeque, pop() is equivalent to removeFirst().
pop() throws NoSuchElementException. Use poll() if you want null instead.
No. EmptyStackException is thrown by the legacy Stack class. ArrayDeque.pop() throws NoSuchElementException.
Use peek(). It returns the head, or null if the deque is empty.

Next: push()

Stack-style insert at the head of the deque.

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.

12 people found this page helpful