Java Stack Class

Java Stack

Java has a stack class that helps in implementing a stack data structure. We can also refer to this class as a subclass of the vector. As we are aware of the fact that stack follows last in first out the principle in addition to push and pop operations. Last in first out (LIFO) means when an element is added to the top of the stack, the element will also be removed from the top of the stack. The stack class in Java has three additional functions which are empty, search and peek.

We need to know certain things about stack before we create one.

  • A stack contains no elements when it is created. We need to push the elements into the stack.
  • The last element pushed inside the stack will be operated first always.
Collection <-- List <-- Vector <-- Stack

Java Stack Methods

Push() Object: It will send the element to the top of the stack. Once you create a stack, you need to have elements to fill the stack. After gathering elements, we use push() operation to fill the stack. The syntax is:

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

Pop() Object: It removes the top element of the stack. When the user calls pop() operation, an exception β€œEmptyStackException” is thrown when the stack is empty. When we push the element into the Java stack class, we can anytime pop it back. To implement the pop() operation, we follow the syntax below:

Stack<String> stack = new Stack< String>( ); 
String top = stack.pop( );

Peek() Object: This operation sends the object to the top but it doesn’t remove the element from there. So, it tends to return the position of an element present at the top.

Stack<String> stack = new Stack< String>( ); 
String top = stack.peek( );

Boolean empty(): This operation will return true when the stack has elements. Otherwise, it will return false.

int search(): We use it to check the presence of an element in the stack. If the search operation finds the element, the object is returned to the top of the stack. Otherwise, it returns -1.

Stack<String> stack = new Stack< String>( ); 
int.index =β€œ20”);

Stack creates an empty space which invokes the default constructor. The package we use for implementing stack class in Java is java.util.Stack. When the user imports this package, the following is the syntax to write stack class with generic type.

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

We have used β€œType” here which indicates the type of the stack.

Stack in Java

import java.util.*;
public class DeveloperHelps {
static void showpush(Stack st, int a) {
      st.push(new Integer(a));
      System.out.println("push(" + a + ")");
      System.out.println("stack: " + st);
static void showpop(Stack st) {
      System.out.print("pop -> ");
      Integer a = (Integer) st.pop();
      System.out.println("stack: " + st);
public static void main(String args[]) {
      Stack st = new Stack();
      System.out.println("stack: " + st);
      showpush(st, 35);
      showpush(st, 42);
      showpush(st, 59);
      try {
      } catch (EmptyStackException e) {
         System.out.println("The stack is empty");

The output of the above program will be:

stack: []
stack: [35]
stack: [35, 42]
stack: [35, 42, 59]
pop -> 59
stack: [35, 42]
pop -> 42
stack: [35]
pop -> 35
stack: []
pop -> The stack is empty

The class inherits the following methods:

  • java.util.vector
  • java.util.AbstractList
  • java.util.Object
  • java.util.List

One comment on “Java Stack

  • KsiΔ…ΕΌki Psychologia , Direct link to comment

    I guess you have made many really interesting points. Not as well many ppl would actually think about it the direction you just did. I am really impressed that there is so much about this subject that has been uncovered and you made it so nicely, with so considerably class. Top-notch one, man! Very special things right here.

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!