Java Stack Class Methods

The Stack class in Java represents a last-in-first-out (LIFO) stack of objects. It extends the Vector class with five operations that allow a vector to be treated as a stack. The stack class provides a means to hold a collection of objects and provides basic methods for pushing, popping, and peeking elements.

This guide covers various methods available in the Stack class. Each method is described in simple terms to help beginners understand how to use them. These methods enable standard stack operations like checking if the stack is empty, viewing the top element without removing it, removing the top element, and searching for an element within the stack.

For more detailed information, please refer to the official Java SE Documentation.

Java Stack Class Methods

The table below contains various methods of the Java Stack class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
empty() Checks if the stack is empty. Returns true if the stack is empty, otherwise false.
peek() Returns the object at the top of the stack without removing it from the stack.
pop() Removes and returns the object at the top of the stack.
push() Pushes an item onto the top of the stack.
search() Returns the 1-based position of an object on the stack. If the object is not found, returns -1.

References:

Comments