Java Programming Handbook

    Java Stack

    The Stack class in Java is a part of the java.util package and extends the Vector class. It represents a Last-In-First-Out (LIFO) data structure, where the last element added is the first one to be removed.

    1. What is Stack?#

    Stack
    • Stack is a subclass of Vector.
    • It works on LIFO (Last in first out )principle. i.e element which is entered last will be removed first.
    • It provides five key methods: push(), pop(), peek(), search(), and empty().
    Stack<Integer> stack = new Stack<>();

    But what is Vector?#

    Since Stack extends Vector, it's important to understand what a Vector is:

    • Vector is a growable array of objects.
    • It is part of the legacy collection classes and was introduced in JDK 1.0.
    • Like ArrayList, Vector uses a dynamic array to store elements.
    • The key difference: Vector is synchronized, which makes it thread-safe, whereas ArrayList is not.

    Why Stack extends Vector?#

    When Stack was introduced in Java, Vector was one of the only dynamic array-based structures that supported synchronization. To build on that functionality, Stack was created by extending Vector and adding LIFO-specific methods like push(), pop(), and peek().

    However, note that today Deque (like ArrayDeque) is often preferred due to better performance and flexibility.

    2. Stack Methods with Examples#

    1. push(E item) – Adds an element to the top#

    import java.util.*; public class StackPushExample { public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); stack.push(10); stack.push(20); stack.push(30); System.out.println("Stack after push: " + stack); } }

    Output:

    Stack after push: [10, 20, 30]

    2. pop() – Removes and returns the top element#

    int popped = stack.pop(); System.out.println("Popped element: " + popped); System.out.println("Stack after pop: " + stack);

    Output:

    Popped element: 30 Stack after pop: [10, 20]

    3. peek() – Returns the top element without removing#

    int top = stack.peek(); System.out.println("Top element: " + top);

    Output:

    Top element: 20

    4. search(Object o) – Searches the element and returns its 1-based position from the top#

    int pos = stack.search(10); System.out.println("Position of 10 from top: " + pos);

    Output:

    Position of 10 from top: 2

    5. empty() – Checks if the stack is empty#

    System.out.println("Is stack empty? " + stack.empty());

    Output:

    Is stack empty? false

    3. Use Cases of Stack#

    • Expression Evaluation (Postfix, Prefix)
    • Undo Mechanism in editors
    • Backtracking Algorithms (e.g., maze solving)
    • Browser History Navigation

    4. Stack vs Other List Implementations#

    FeatureStackVectorArrayListLinkedList
    Internal StructureDynamic ArrayDynamic ArrayDynamic ArrayDoubly Linked List
    Thread-SafeYesYesNoNo
    OrderLIFOInsertionInsertionInsertion
    Special Opspush, pop, peekNoneNoneadd/remove first/last

    Conclusion#

    The Stack class is a useful utility for handling LIFO-based data structures in Java. Though it's a legacy class and newer alternatives like Deque (via ArrayDeque) are often preferred, it's still important to understand Stack due to its simple usage and appearance in many interview problems. In the next blog, we will explore Queue Interface in Java and its implementations!

    Last updated on Apr 09, 2025