GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Stack Class in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Stack Class in Java

Last Updated : 23 Jul, 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 class implements List, RandomAccess, Cloneable, and Serializable interfaces.
Java
import java.util.Stack;

public class Geeks {
    public static void main(String[] args)
    {
        // Create a new stack
        Stack<Integer> s = new Stack<>();

        // Push elements onto the stack
        s.push(1);
        s.push(2);
        s.push(3);
        s.push(4);

        // Pop elements from the stack
        while (!s.isEmpty()) {
            System.out.println(s.pop());
        }
    }
}

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

To create a Stack, create an object of the Stack class. The Stack class provides only one constructor. 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<>();

Note: Although Stack extends Vector, it does not inherit Vector constructors such as Vector(int initialCapacity). Therefore, constructors like Stack(int initialCapacity) do not exist and cannot be used.

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.

Java
import java.io.*;
import java.util.*;

class Geeks {

    public static void main(String[] args)
    {
        // Default initialization of Stack
        Stack stack1 = new Stack();

        // Initialization of Stack using Generics
        Stack<String> stack2 = new Stack<String>();

        // pushing the elements
        stack1.push("4");
        stack1.push("All");
        stack1.push("Geeks");

        stack2.push("Geeks");
        stack2.push("For");
        stack2.push("Geeks");

        // Printing the Stack Elements
        System.out.println(stack1);
        System.out.println(stack2);
    }
}

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. 

Java
import java.io.*;
import java.util.*;

public class Geeks {

    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack<String> stack = new Stack<String>();

        // Use push() to add elements into the Stack
        stack.push("Welcome");
        stack.push("To");
        stack.push("Geeks");
        stack.push("For");
        stack.push("Geeks");

        // Displaying the Stack
        System.out.println("Initial Stack: " + stack);

        // Fetching the element at the head of the Stack
        System.out.println("The element at the top of the"
                           + " stack is: " + stack.peek());

        System.out.println("Final Stack: " + 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.

Java
import java.io.*;
import java.util.*;

public class Geeks {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack<Integer> stack = new Stack<Integer>();

        stack.push(10);
        stack.push(15);
        stack.push(30);
        stack.push(20);
        stack.push(5);

        System.out.println("Initial Stack: " + stack);

        // Removing elements using pop() method
        System.out.println("Popped element: "
                           + stack.pop());
        System.out.println("Popped element: "
                           + stack.pop());

        // Displaying the Stack after pop operation
        System.out.println("Stack after pop operation "
                           + stack);

        System.out.println("Is stack empty? "
                           + stack.isEmpty());
        // Pop remaining elements
        stack.pop();
        stack.pop();
        stack.pop();

        // Check if the stack is empty
        System.out.println("Is stack empty? "
                           + stack.empty());
    }
}

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.

Java
import java.util.*;

class Geeks {
    public static void main(String[] args) {

        // Stack implementation
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);

        System.out.println("Using Stack (LIFO):");
        while (!stack.isEmpty()) {
            System.out.print(stack.pop() + " "); // pops elements in LIFO order
        }
        System.out.println();

        // ArrayDeque as stack
        Deque<Integer> deque = new ArrayDeque<>();
        deque.push(1);
        deque.push(2);
        deque.push(3);

        System.out.println("Using ArrayDeque (LIFO):");
        while (!deque.isEmpty()) {
            System.out.print(deque.pop() + " "); // pops elements in LIFO order
        }
        System.out.println();
    }
}

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

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.