java util Stack Class
Stack Class in Java
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top. When a stack is first created, it contains no items.
The Stack class extends Vector which implements the List interface. A Vector is a re-sizable collection. It grows its size to accommodate new elements and shrinks the size when the elements are removed. Since the Stack class extends Vector, it also grows and shrinks its size as needed when new elements are added or removed, in other words push and pop.
What is Stack in Java ?
A Stack is a Last In First Out (LIFO) data structure. It supports two basic operations called push and pop. The push operation adds an element at the top of the stack, and the pop operation removes an element from the top of the stack. Java provides a Stack class which models the Stack data structure. The Stack class is part of Java collections framework.
In this tutorial you can learn about java.util.Stack class and its examples. And also learn how to use java.util.Stack class.
java.util.Stack class Example
java.util.Stack class Example 2