Back to Study Material Java

Stack Class in Java

Java's Stack class extends Vector and adds five methods that implement classic LIFO (last-in, first-out) stack semantics: push(E item) adds an element to t...

Stack Class in Java

Stack as a Subclass of Vector

Java's Stack class extends Vector and adds five methods that implement classic LIFO (last-in, first-out) stack semantics: push(E item) adds an element to the top, pop() removes and returns the top element, peek() returns the top without removing it, empty() checks if the stack has no elements, and search(Object o) returns the 1-based position from the top of the stack (returning -1 if absent).

Because Stack extends Vector, it inherits all of Vector's methods including the synchronized ones. It also inherits Vector's ability to access elements by index, insert in the middle, and iterate in any order, which are all non-stack operations. This inheritance-based design violates the principle that a subclass should be a true behavioral subtype: a stack should expose only LIFO access, but Java's Stack allows arbitrary indexed access, breaking the abstraction.

The search method runs in O(n) time, scanning from top to bottom.

Typical Use Cases

Stacks are useful whenever you need to reverse a sequence, evaluate expressions, or track nested structure. Classic applications include parsing arithmetic expressions with operator precedence, implementing undo/redo functionality in editors, managing function call frames in interpreters, and performing depth-first traversal of graphs and trees iteratively.

In all these scenarios, the fundamental pattern is the same: push elements as you encounter them and pop them when you need to process or revisit the most recently seen item. The recursive call stack in a JVM is conceptually the same structure, just managed by the runtime rather than your code.

For expression evaluation, you typically maintain two stacks: one for operands and one for operators. When you encounter a closing parenthesis or a lower-precedence operator, you pop and compute until the condition resolves.

Preferred Alternative and Interview Tip

The Java documentation itself recommends using Deque instead of Stack for new code. The idiomatic choice is ArrayDeque, which is unsynchronized and backed by a resizable circular array. ArrayDeque.push(e) maps to addFirst, and ArrayDeque.pop() maps to removeFirst. Both are O(1) amortized. ArrayDeque is generally faster than Stack because it avoids synchronization overhead and benefits from better memory layout.

A short example: Deque<Integer> stack = new ArrayDeque<>(); stack.push(1); stack.push(2); stack.push(3); System.out.println(stack.pop()); prints 3, confirming LIFO order.

An interview tip: mention that choosing Deque over Stack shows awareness of Java's API evolution and an understanding that inheritance is a strong coupling mechanism that should not be used merely for code reuse. The Stack-extends-Vector relationship is a well-known example of misuse of inheritance cited in effective Java design discussions.