Stack Class in Java

Java provides an inbuilt object type called Stack. It is a collection that is based on the last in first out (LIFO) principle. On Creation, a stack is empty.

It extends Vector class with five methods that allow a vector to be treated as a stack. The five methods are:

1. Object push(Object element) : Pushes an element on the top of the stack.

2. Object pop() : Removes and returns the top element of the stack. An ‘EmptyStackException’ exception is thrown if we call pop() when the invoking stack is empty.

3. Object peek( ) : Returns the element on the top of the stack, but does not remove it.

4. boolean empty() : It returns true if nothing is on the top of the stack. Else, returns false.

5. int search(Object element) : It determines 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.


// Java code for stack implementation

import java.util.*;

class Test
    // Pushing element on the top of the stack
    static void stack_push(Stack<Integer> stack)
        for(int i = 0; i < 5; i++)
    // Popping element from the top of the stack
    static void stack_pop(Stack<Integer> stack)
        System.out.println("Pop :");

        for(int i = 0; i < 5; i++)
            Integer y = (Integer) stack.pop();

    // Displaying element on the top of the stack
    static void stack_peek(Stack<Integer> stack)
        Integer element = (Integer) stack.peek();
        System.out.println("Element on stack top : " + element);
    // Searching element in the stack
    static void stack_search(Stack<Integer> stack, int element)
        Integer pos = (Integer);

        if(pos == -1)
            System.out.println("Element not found");
            System.out.println("Element is found at position " + pos);

    public static void main (String[] args)
        Stack<Integer> stack = new Stack<Integer>();

        stack_search(stack, 2);
        stack_search(stack, 6);

Output :

Pop :
Element on stack top : 4
Element is found at position 3
Element not found

This article is contributed by Mehak Narang.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Please write to us at to report any issue with the above content.

Recommended Posts:

1.7 Average Difficulty : 1.7/5.0
Based on 17 vote(s)