Stack
A stack in Java represents a last-in, first-out (LIFO) data structure for storing objects. It is implemented as the Stack
class within the java.util
package and extends the Vector
class. A stack allows operations such as pushing items to the top, popping items off the top, and peeking at the top item without removing it.
Stacks are commonly used in programming problems involving recursion, expression evaluation, undo functionality, and backtracking algorithms.
A common use of stacks is implementing the undo feature in text editors. Each action a user takes (e.g., typing a character) is pushed onto a stack. When the undo command is triggered, the most recent action is popped from the stack and reversed.
To use the Stack
class, it must first be imported using:
import java.util.Stack;
Since Java 1.6, the Deque
interface has provided a more modern and efficient way to implement LIFO stack operations. According to the JDK documentation, Deque
should be used in preference to the legacy Stack
class.
Stack method |
Equivalent Deque method |
---|---|
.push(item) |
.addFirst(item) |
.pop() |
.removeFirst() |
.peek() |
.getFirst() |
Syntax
Stack<Type> stackName = new Stack<>();
Parameters:
Type
: The data type of elements stored in the stack (e.g.,Integer
,String
).stackName
: The variable name of theStack
object.
Note: Java stacks are generic, meaning they can store any object type specified in angle brackets.
Common Stack Methods
The Stack
class provides the following methods:
.push(item)
: Adds an item to the top of the stack..pop()
: Removes and returns the item from the top of the stack..peek()
: Returns the item at the top without removing it..empty()
: Returnstrue
if the stack is empty..search(item)
: Returns the 1-based position from the top of the stack if found; otherwise, returns -1.
Example: Basic Stack Operations
The following example demonstrates how to use common stack methods such as .push()
, .pop()
, .peek()
, .empty()
, and .search()
:
import java.util.Stack;public class StackExample {public static void main(String[] args) {Stack<String> books = new Stack<>();// Push items onto the stackbooks.push("Java");books.push("Python");books.push("C++");// Peek at the top itemSystem.out.println("Top item: " + books.peek()); // C++// Pop the top itembooks.pop();// Check if stack is emptySystem.out.println("Is stack empty? " + books.empty());// Search for an itemSystem.out.println("Position of Java: " + books.search("Java"));}}
This example will result in the following output:
Top item: C++Is stack empty? falsePosition of Java: 2
Frequently Asked Questions
1. What is a stack in Java?
A stack is a linear data structure that follows the last-in, first-out (LIFO) principle. In Java, it is represented by the Stack
class in the java.util
package.
2. How do you implement a stack in Java?
A stack can be implemented using the built-in Stack
class or manually using arrays or linked lists. The simplest approach is:
Stack<Integer> stack = new Stack<>();
3. What is the difference between stack and queue in Java?
A stack uses LIFO (last-in, first-out) ordering, while a queue uses FIFO (first-in, first-out). This means stacks remove the most recently added element, while queues remove the oldest one.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours