VOOZH about

URL: https://www.geeksforgeeks.org/java/stack-class-in-java/

⇱ Stack Class in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Stack Class in Java

Last Updated : 24 Apr, 2026

In Java, a Stack is a linear data structure that follows the Last In First Out (LIFO) principle and is defined in the java.util package. Internally, it extends the Vector class.

  • Stack class maintains insertion order and allows duplicates and null values.
  • Grows dynamically when its capacity is exceeded.
  • Stack methods are synchronized.
  • Stack class implements List, RandomAccess, Cloneable, and Serializable interfaces.

Output
4
3
2
1

Explanation:

  • Elements are added to the stack using the push() method, placing each item on top.
  • A loop runs while the stack is not empty, checked using the isEmpty() method.
  • Elements are removed and printed using the pop() method in LIFO order.

Note: The Stack class is a legacy class; in modern Java development, use ArrayDeque or LinkedList instead.

Hierarchy of Stack class

Stack class extends Vector, which extends Object

πŸ‘ Stack Class in Java

Constructors of Stack Class

In order to create a Stack, we need to create an object of the Stack class. The Stack class inherits constructors from the Vector class.

1. Stack()

This constructor is used to create an empty stack.

Stack<Integer> stack = new Stack<>();

2. Stack(int initialCapacity)

This constructor is used to create a stack with a specified initial capacity.

Stack<Integer> stack = new Stack<>();

Performing Different Operations on Stack Class

1. Adding Elements

With the help of push() method we can add element to the stack. The push() method place the element at the top of the stack.


Output
[4, All, Geeks]
[Geeks, For, Geeks]

2. Accessing the Element

With the help of peek() method we can fetch the top element of the stack. 


Output
Initial Stack: [Welcome, To, Geeks, For, Geeks]
The element at the top of the stack is: Geeks
Final Stack: [Welcome, To, Geeks, For, Geeks]

3. Removing Elements

With the help of pop() method we can delete and return the top element from the stack.


Output
Initial Stack: [10, 15, 30, 20, 5]
Popped element: 5
Popped element: 20
Stack after pop operation [10, 15, 30]
Is stack empty? false
Is stack empty? true

Prioritize Use of Deque over Stack

The Stack class in Java is inherits from Vector in Java. It is a thread-safe class. It is recommended to use ArrayDeque for stack implementation as it is more efficient in a single-threaded environment.


Output
Using Stack (LIFO):
3 2 1 
Using ArrayDeque (LIFO):
3 2 1 

Methods in Stack Class

Method

Description

empty()

This method returns true if the stack contains no elements at all, and false otherwise. It does not check the β€œtop” element specifically.

peek()

This method returns the element on the top of the stack, but does not remove it.

pop()

This method removes and returns the top element of the stack.

push(Object element)

This method pushes an element on the top of the stack.

search(Object element)

This method is used to determine whether an object exists in the stack. If the element is found. It returns the position of the element from the top of the stack. Else, it returns -1.

Comment