Let us learn what is Stack class in Java programming with its explanation, constructors, methods, implementation and output.
What is Stack Class?
The Stack class is a predefined class available in java.util package. This class implements a dynamic stack data structure.
A stack is basically an ordered list of elements with a similar data type, and stores elements within it. It is a linear data structure and works on LIFO (Last In First Out) principle.

Image Source: Wikimedia
The Stack class in Java inherits from the Vector class. There are a handful of operations that can be performed on a stack which is enlisted below.
- Push: Inserting an element into the stack.
- Pop: Removing an element from the stack.
- Peek: Returning the element at the topmost index in the stack.
- Search: Searching an element in the stack.
The elements are inserted and deleted from the same end which is commonly known as TOP. So, the TOP is the reference pointer in a stack which helps to execute all the operations.
The element which is inserted as the last element gets removed as the first element since it gets stored at the TOP index. This is due to the LIFO structure.
A stack is said to be in an underflow state when it is completely empty and it appears to be in an overflow state when it is completely full.
Declaration of Stack Class
1 | public class Stack extends Vector |
Constructors of Stack Class
SR. NO. | CONSTRUCTOR | DESCRIPTION |
---|---|---|
1. | Stack() | This method constructs an empty stack. |
Methods of Stack Class
SR. NO. | METHOD | DESCRIPTION |
---|---|---|
1. | boolean empty() | This method checks if the stack is empty or not. If it is empty then it returns true, else false. |
2. | element pop() | This method deletes the topmost element from the stack and returns it. |
3. | element push(element obj) | This method inserts an element into stack starting from the topmost index and returns the element. |
4. | element peek() | This method returns the topmost element from the stack without deleting or modifying it in any way. |
5. | int search(Object element) | This method returns the position of the element available within the stack based on the element passed in the method parameter. If the element is not found then it returns -1. |
Implementation of Vector Class in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.*; class Sample { public static void main(String args[]) { int count = 0; Stack st = new Stack(); System.out.println("\nDefault Stack:\t" + st); for(count = 0; count < 5; count++) { byte element = (byte)count; st.push(element); System.out.println("Stack:\t" + st); } while(!st.empty()) { System.out.println("\nPopped Element:\t" + st.pop()); System.out.println(st); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Default Stack: [] Stack: [0] Stack: [0, 1] Stack: [0, 1, 2] Stack: [0, 1, 2, 3] Stack: [0, 1, 2, 3, 4] Stack: [0, 1, 2, 3, 4, 5] Popped Element: 5 Stack: [0, 1, 2, 3, 4] Popped Element: 4 Stack: [0, 1, 2, 3] Popped Element: 3 Stack: [0, 1, 2] Popped Element: 2 Stack: [0, 1] Popped Element: 1 Stack: [0] Popped Element: 0 Stack: [] |
If you have any doubts about the implementation of Stack class in Java programming, let us know about it in the comment section. Find more on Oracle Docs.
Collection Frameworks |
---|
StringTokenizer Class |
Vector Class |
Random Class |