This post explains what is java.util.Stack data structure, discusses the most common operations performed on Stack. A diagrammatic representation of how the operations work internally and a code snippet for the same.
Prerequisites:
Basic knowledge of Java and basic knowledge of data structures.
Explanation :
java.util.Stack data structure class extends Vector class with operations to support LIFO (Last In First Out). Stack data structure uses a pointer called as Top to keep track of the topmost element of the stack. Stack class has a default no-argument constructor.
Creating an empty instance of stack class creates a stack of default size = 10. If more elements are pushed then the size will be doubled automatically.
Stack<Integer> stack = new Stack<Integer>(); //this creates a stack of size = 10
Stack common operations :
- public Object push(Object item) : Pushes an element item to the top of the stack and returns it. This operation is performed at the Top of the stack.
- public Object pop() : Removes an element from the top of the stack and returns it. This operation is performed at the Top of the stack.
- public Object peek() : Returns the topmost element from the stack but does not remove it. This operation is performed at the top of the stack.
- public boolean isEmpty() : Returns true if the stack is empty, else returns false.
- public int search(Object o) : Returns 1-based position of the element from the top of the stack. The item at the top of the stack is considered to be at position 1, next item at position 2 and so on. If the element is not found then it returns -1.
- public int size() : Returns the number of elements present in the stack.
- public int capacity() : Returns the current capacity of the stack. For example, capacity() when called on the empty stack it returns 10.
Diagrammatic representation of stack :

Example code :
import java.util.Stack;
class StackCommonOperations {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>(); // creates an empty stack of capacity 10
int itemPushed1 = stack.push(1); // push an item and return it
int itemPushed2 = stack.push(2);
int itemPushed3 = stack.push(3);
int capacity1 = stack.capacity(); // returns 10
System.out.println("Current stack capacity i.e. capacity1 = " + capacity1);
int size1 = stack.size(); // returns 3
System.out.println("Current stack size i.e. size1 = " + size1);
int itemPopped = stack.pop(); // removes and returns 3
System.out.println("Item popped itemPopped = " + itemPopped);
int itemPeeked = stack.peek(); // returns 2
System.out.println("Item peeked itemPeeked = " + itemPeeked);
boolean isStackEmpty = stack.isEmpty(); // returns false
System.out.println("Is stack empty = " + isStackEmpty);
int itemSearched1 = stack.search(1); // returns 2
System.out.println("Search 1 position = " + itemSearched1);
int itemSearched2 = stack.search(2); // returns 1
System.out.println("Search 2 position = " + itemSearched2);
}
}
Output of above code :
Current stack capacity i.e. capacity1 = 10
Current stack size i.e. size1 = 3
Item popped itemPopped = 3
Item peeked itemPeeked = 2
Is stack empty = false
Search 1 position = 2
Search 2 position = 1
Here is the link to my github profile for the above code, Stack common operations.
Happy learning..!!
Please do give a like if you find the post helpful. Thank you for reading.
4 thoughts on “java.util.Stack common operations”