STACKS IN JAVA
Published: 2021-11-25 12:00:15

A Stack in Java is a collection (or group) of elements stored in the last in first out (LIFO) order. In other words, a stack is a data structure that stores data in last-in, first-out fashion.

This means an element that is stored as a last element into the stack, will be the first element to be removed from the stack. Only the top element on the stack is accessible at a given time.

When an element (object) is inserted into the stack, it is called push operation. We can create a stack of any type of elements.

When an element is removed from the stack, it is called pop operation.

When an element is searched in the stack, it is called operation.

Insertion and deletion of elements take place only from one side of the stack, traditionally called the top of the stack. That is, new elements are added to the top of the stack, and elements are removed from the top of the stack.

Therefore, stack is called last-in, first-out data structure. A typical example of stack in java is shown in the below figure.

Java Stack

As you can see in the above figure, we have added three elements A, B, and C into the stack. When they are removed from the stack, we obtain elements C, B, and A fashion. This fashion is called Last-In, First-Out data structure.

Let’s understand it with real-time examples to clear more.

**Realtime Example of Stack in Java**
1. A real-time example of stack is a stack of books as shown in the below figure. We can not take a book from the stack without removing books that are first stacked on top of it.

Realtime example of stack in java

2. A second real-time example of stack is a pile of plates in a cafeteria where last washed plate will be out first for use.

3. Similarly, a DVD disk holder where CDs are arranged in such that the last CD will be out first for use. This is also an example of stack.

**Stack Class declaration**
Stack is a generic class in java that has the following declaration in a general form:

public class Stack<E>
        extends Vector<E>
Here, E represents the type of elements that stack can hold.

**Features of Stack class**
There are several features of stack in Java that are as follows:

1. Stack is a group of elements with “last-in, first-out” retrieval.

2. In the Java stack, all operations take place at the top of the stack.

3. Push operation inserts an element to the top of stack.

4. Pop operation removes an element from the stack and returns it.

5. Stack class is synchronized. That means it is thread-safe.

6. Null elements are allowed into the stack.

7. Duplicate elements are allowed into the stack.

**Stack Methods in Java**
In addition to methods that inherit from vector class, stack class has five additional methods of its own. They are as follows:

1. Boolean empty(): This method is used to check the stack is empty or not. It returns true if and only if stack contains no elements (objects). Otherwise, it returns false.

2. E peek(): This method is used to retrieve the top-most element from the stack without removing it.

3. E pop(): The pop() method is used to pop (remove) the top-most element from the stack and returns it.

4. E push(E obj): This method pushes an element obj onto the top of the stack and returns that element (object).