Java Stack Program
Summary: By the end of this tutorial "Java Stack Program", you will understand stack in Java. Very comfortable with the explaination and code examples.
Stack, in any language, stores elements in LIFO (Last-in First-out) order. As the name indicates, in LIFO, the last added element is retrieved first. It can be imagined like a stack of plates put on the top of the other. The plate added at the end is drawn first. In stack, elements cannot be added in the middle or removed. There can be only one point of entry and one point of exit.
Following is the class signature
Java Stack is derived from Vector and again Vector is derived from List. List belongs to collections framework (JDK 1.2) and Stack and Vector belong to legacy classes (JDK 1.0 DS are known as legacy classes; they are Stack, Vector, Hashtable and Properties).
The Java designers extended List to Stack and Vector to give the features and advantages of collections (JDK 1.2 DS) to legacy classes (JDK 1.0 DS). It is a good designing principle. Now stack and vector can use all the methods of Collection and List interfaces.
The java.util.Stack comes with five operations represented by five methods.
- Object push(Object obj): An element obj is added the to the stack with push() method. Elements are added implicitly on the top of another. The method returns the element added.
- Object pop(): With this method, the elements can be retrieved from the Stack. The last one added (which lies on the top) is returned first. With pop(), the element comes out of the stack permanently. That is, it cannot be retrieved again.
- int search(Object obj): With this method, an element, obj, index number can be obtained. The counting starts from the top. The topmost element index number is 1. If the stack is empty, the method returns EmptyStackException.
- Object peek(): This method returns the element existing on the top. But element is not popped (removed) out. This method is best useful to know whether an element exists or not.
- boolean empty(): Returns true if no elements exist in the stack.
pop vs peek
For a beginner, these two methods are very confusing. Let us discuss again. The pop() method returns the element existing on the top of the stack. The element returned is deleted from the stack permanently. That is, the programmer cannot obtain it again.
The peek() method also returns the element but the element is not deleted from the stack. That is, calling peek() method five times, returns the same element 5 times. peek() method can be used to know which element is lying on the top of the stack or which element will returned when pop() is called. It can be used in regular programming like this.
Limitations of Stack Usage
There are two limitations for stack usage.
- The programmer stores data in a DS with an intention to get the elements whenever he would like in the code. But with stack, the popped element is completely removed and cannot be obtained again. For this reason stack is not used much in general production software like health and banking etc, but used by system and tool developers.
- Another drawback is, the elements cannot be added or retrieved from the middle.
The Java Stack class comes with only one constructor.
- Stack(): This default constructor creates an empty Stack object.