Open links in new tab
    • Work Report
    • Email
    • Rewrite
    • Speech
    • Title Generator
    • Smart Reply
    • Poem
    • Essay
    • Joke
    • Instagram Post
    • X Post
    • Facebook Post
    • Story
    • Cover Letter
    • Resume
    • Job Description
    • Recommendation Letter
    • Resignation Letter
    • Invitation Letter
    • Greeting Message
    • Try more templates
  1. The Stack class in Java is a part of the Java Collection Framework and represents a last-in-first-out (LIFO) stack of objects. It extends the Vector class and provides several methods to perform standard stack operations such as push, pop, peek, empty, and search.

    Key Methods of Stack Class

    1. push(E item)

    The push method adds an item to the top of the stack. This method is equivalent to the addElement method of the Vector class.

    Stack<Integer> stack = new Stack<>();
    stack.push(1);
    stack.push(2);
    System.out.println(stack); // Output: [1, 2]
    Copied!

    2. pop()

    The pop method removes and returns the object at the top of the stack. If the stack is empty, it throws an EmptyStackException.

    int topElement = stack.pop();
    System.out.println(topElement); // Output: 2
    System.out.println(stack); // Output: [1]
    Copied!

    3. peek()

    The peek method returns the object at the top of the stack without removing it. If the stack is empty, it throws an EmptyStackException.

    Feedback
  2. Stack Class in Java - GeeksforGeeks

    Mar 23, 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 …

  3. Quick Guide to Java Stack | Baeldung

    • This tutorial is a quick and practical guide to understand this core class in Java – the Stack. Of course, you can explore the full API in the Javadoc. And, as always, all code samples can be found over on GitHub.
    See more on baeldung.com
    • Published: Dec 21, 2019
    • Java Stack Class - Programiz

      In this tutorial, we will learn about the Java Stack class and its methods with the help of examples.

    • Java Stack Tutorial: Stack Class Implementation With Examples

      • See More

      Apr 1, 2025 · This Tutorial Explains What is Stack in Java, Java Stack Class, Stack API Methods, Stack Implementation using Array & Linked List with the help of Examples.

    • Java Stack class - Tpoint Tech

      Feb 13, 2026 · The stack is a linear data structure that is used to store the collection of objects. It is based on Last-In-First-Out (LIFO).

    • Java - The Stack Class - Online Tutorials Library

      Stack is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, …

    • Stack.java - Princeton University

      Nov 26, 2022 · Below is the syntax highlighted version of Stack.java from §4.3 Stacks and Queues. /****************************************************************************** * Compilation: javac …

    • Java | Stack | Codecademy

      Nov 21, 2022 · A stack in Java represents a last-in, first-out (LIFO) data structure for storing objects. It is implemented as the Stack class within the java.util package …

    • Java Stack - Jenkov.com

      May 21, 2020 · The Java Stack class is a classical stack data structure, where elements can be pushed onto the top of the stack, and popped off the top again. …