Stack Class in Java

Java Collection framework provides a Stack class that models and implements a Stack data structure. The class is based on the basic principle of last-in-first-out.

Introduction

 
Stack is a subclass of Vector that implements a standard last-in, first-out stack in Java. In this article, we will learn about Java Stack class, its methods, and constructors that are provided in the Java programming language.
 

What is Stack class in Java?

 
The Stack represents a last-in, first-out (LIFO) data structure and linear data structure. A stack has two fundamental operations, called push and pop. It extends the Vector class 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.
 
java.lang.Object
    java.util.Vector
        java.util.Stack
 
As Vector implements List, Stack class is also a List implementation class but does NOT support all operations of Vector or List. As Stack supports LIFO, it is also known as LIFO Lists.
 
java-stack-class 
 

What is the LIFO mechanism?

 
The Stack data structure follows the LIFO structure (Last In First Out). LIFO is a method of processing data in which the last items entered are the first to be removed. This is the opposite of LIFO is FIFO (First In, First Out), in which items are removed in the order they have been entered.
 
To better understand LIFO, imagine stacking a deck of cards by placing one card on top of the other, starting from the bottom. Once the deck has been fully stacked, you begin to remove the cards, starting from the top. This process is an example of the LIFO method because the last cards to be placed on the deck are the first ones to be removed.
 

Constructor of Stack Class in Java

 
Stack only defines the default constructor, which creates an empty stack. The stack includes all the methods defined by Vector and adds several of its own.
 

Stack()

 
The Stack class supports one default constructor Stack() which is used to create an empty stack. The complete program of the Stack() constructor is listed below.
  1. public class StackConstructorExample {  
  2.     StackConstructorExample() {  
  3.         System.out.println("Empty stack is created");  
  4.     }  
  5.     public static void main(String args[]) {  
  6.         StackConstructorExample stack = new StackConstructorExample();  
  7.     }  
  8. }
The above program generates the following output.
 
stack-constructor-example-output 
 

Methods of Stack class in Java

 

1) Stack push() method in Java

 
The Java.util.Stack.push(E element) method is used to push an element into the Stack. The element gets pushed onto the top of the Stack. The method accepts one parameter element of type Stack and refers to the element to be pushed into the stack. The method returns the argument passed.
 
The program adds the string value into the Stack. The complete program of the Stack.push() method is listed below.
  1. import java.util.*;  
  2. public class PushMethodExample {  
  3.     public static void main(String args[]) {  
  4.         // Creating an empty Stack  
  5.         Stack<String> stack = new Stack<String>();  
  6.         stack.push("Welcome");  
  7.         stack.push("To");  
  8.         stack.push("C#Corner");  
  9.         System.out.println("Initial Stack: " + stack);  
  10.         // Push elements into the stack  
  11.         stack.push("Hello");  
  12.         stack.push("Programmers");  
  13.         // Displaying the final Stack  
  14.         System.out.println("Final Stack: " + stack);  
  15.     }  
The above program generates the following output.
 
push-method-example-output 
 
The program adds the integer value into tha Stack. The complete program of Stack.push() method is listed below.
  1. import java.util.*;  
  2. public class PushElementExample2 {  
  3.     public static void main(String args[]) {  
  4.         // Creating an empty Stack  
  5.         Stack<Integer> stack = new Stack<Integer>();  
  6.         // Use push() to add elements into the Stack  
  7.         stack.push(10);  
  8.         stack.push(15);  
  9.         stack.push(30);  
  10.         stack.push(20);  
  11.         stack.push(5);  
  12.         System.out.println("Initial Stack: " + stack);  
  13.         // Pushing elements into the Stack  
  14.         stack.push(1254);  
  15.         stack.push(4521);  
  16.         System.out.println("Final Stack: " + stack);  
  17.     }  
The above program generates the following output.
 
push-method-example-output 
 

2) Stack pop() Method in Java

 
The Java.util.Stack.pop() method in Java is used to pop an element from the stack. The element is popped from the top of the stack and is removed from the same. The method does not take any parameter. This method returns the element present at the top of the stack and then removes it. The method throws EmptyStackException is thrown if the stack is empty.
 
The program adds the string value into the Stack. The complete program of the Stack.push() method is listed below.
  1. import java.util.*;  
  2. public class PopMethodExample {  
  3.     public static void main(String args[]) {  
  4.         // Creating an empty Stack  
  5.         Stack<String> stack = new Stack<String>();  
  6.         stack.push("Welcome");  
  7.         stack.push("To");  
  8.         stack.push("C#Corner");  
  9.         System.out.println("Initial Stack: " + stack);  
  10.         System.out.println("Popped element: " + stack.pop());  
  11.         System.out.println("Popped element: " + stack.pop());  
  12.         // Displaying the Stack after pop operation  
  13.         System.out.println("Stack after pop peration " + stack);  
  14.     }  
The above program generates the following output.
 
pop-example-method-example 
 
The program add the integer value into tha Stack. The complete program of Stack.push() method is listed below. 
  1. import java.util.*;  
  2. public class PopMethodExample2 {  
  3.     public static void main(String args[]) {  
  4.         // Creating an empty Stack  
  5.         Stack<Integer> STACK = new Stack<Integer>();  
  6.         STACK.push(10);  
  7.         STACK.push(15);  
  8.         STACK.push(30);  
  9.         STACK.push(20);  
  10.         STACK.push(5);  
  11.         System.out.println("Initial Stack: " + STACK);  
  12.         // Removing elements using pop() method  
  13.         System.out.println("Popped element: " + STACK.pop());  
  14.         System.out.println("Popped element: " + STACK.pop());  
  15.         // Displaying the Stack after pop operation  
  16.         System.out.println("Stack after pop operation " + STACK);  
  17.     }  
The above program generates the following output.
 
pop-example-method-example 
 

3) Stack peek() Method in Java

 
The java.util.Stack.peek() method in Java is used to retrieve or fetch the first element of the Stack or the element present at the top of the Stack. The element retrieved does not get deleted or removed from the Stack. The method does not take any argument. The method returns the element at the top of the Stack else it returns NULL if the Stack is empty. Exception: The method throws EmptyStackException if the stack is empty.
 
The program adds the string value into the Stack. The complete program of the Stack.push() method is listed below.
  1. import java.util.Stack;  
  2. public class PeekMethodExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<String> stack = new Stack<String>();  
  5.         stack.push("Welcome");  
  6.         stack.push("To");  
  7.         stack.push("C#corner");  
  8.         System.out.println("Initial Stack: " + stack);  
  9.         System.out.println("The element at the top of the" + " stack is: " + stack.peek());  
  10.         System.out.println("Final Stack: " + stack);  
  11.     }  
The above program generates the following output.
 
peek-example-output  
 
The program adds the integer value into the Stack. The complete program of the Stack.push() method is listed below.
  1. import java.util.*;  
  2. public class PeekMethodExample2 {  
  3.     public static void main(String args[]) {  
  4.         Stack<Integer> stack = new Stack<Integer>();  
  5.         stack.push(10);  
  6.         stack.push(15);  
  7.         stack.push(30);  
  8.         stack.push(20);  
  9.         stack.push(5);  
  10.         System.out.println("Initial Stack: " + stack);  
  11.         System.out.println("The element at the top of the" + " stack is: " + stack.peek());  
  12.         System.out.println("Final Stack: " + stack);  
  13.     }  
The above program generates the following output.
 
 
 

4) Stack empty() Method in Java

 
The java.util.Stack.empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false. The method does not take any argument. The method returns boolean true if the stack is empty else it returns false.
 
The complete program of Stack.empty() method example, for checking the Stack is empty or not. 
  1. import java.util.*;  
  2. public class EmptyMethodExample {  
  3.     public static void main(String[] args) {  
  4.         Stack<String> STACK = new Stack<String>();  
  5.         System.out.println("Is the stack empty? " + STACK.empty());  
  6.     }  
The above program generates the following output.
 
empty-example-output 
 
Another program of Stack.empty() method example, for checking if the Stack is empty or not.
  1. import java.util.*;  
  2.   
  3. public class EmptyMethodExample2 {  
  4.     public static void main(String[] args) {  
  5.         // Creating an empty Stack   
  6.         Stack<Integer> stack = new Stack<Integer>();  
  7.         // Stacking int values   
  8.         stack.push(8);  
  9.         stack.push(5);  
  10.         stack.push(9);  
  11.         stack.push(2);  
  12.         stack.push(4);  
  13.         System.out.println("The stack is: " + stack);  
  14.         System.out.println("Is the stack empty? " + stack.empty());  
  15.     }  
The above program generates the following output.
 
empty-example-output2 
 

5) Stack search() Method in Java

 
The java.util.Stack.search(Object element) method in Java is used to search for an element in the stack and get its distance from the top. This method starts the count of the position from 1 and not from 0. The element that is on the top of the stack is considered to be at position 1. If more than one element is present, the index of the element closest to the top is returned. The method returns its position if the element is successfully found and returns -1 if the element is absent.
 
The method accepts one parameter element which refers to the element that is required to be searched for in the Stack. The method returns the position of the element if it is successfully found in the stack (taking the count as base 1) else -1 is returned.
 
The complete program of Stack.search() method is listed below.
  1. import java.util.*;  
  2. public class SearchMethodExample {  
  3.     public static void main(String[] args) {  
  4.         Stack<String> STACK = new Stack<String>();  
  5.         STACK.push("Hello");  
  6.         STACK.push("4");  
  7.         STACK.push("C#corner");  
  8.         STACK.push("Welcomes");  
  9.         STACK.push("You");  
  10.         System.out.println("The stack is: " + STACK);  
  11.         System.out.println("Does the stack contains '4'? " + STACK.search("4"));  
  12.         System.out.println("Does the stack contains 'Hello'? " + STACK.search("Hello"));  
  13.     }  
The above program generates the following output.
 
search-method-example 
 
Another program of Stack.search() method example is listed below.
  1. import java.util.*;  
  2. public class SearchMethodExample2 {  
  3.     public static void main(String[] args) {  
  4.         Stack<Integer> STACK = new Stack<Integer>();  
  5.         STACK.push(8);  
  6.         STACK.push(5);  
  7.         STACK.push(9);  
  8.         STACK.push(2);  
  9.         STACK.push(4);  
  10.   
  11.         System.out.println("The stack is: " + STACK);  
  12.         System.out.println("Does the stack contains '9'? " + STACK.search(9));  
  13.         System.out.println("Does the stack contains '10'? " + STACK.search(10));  
  14.     }  
The above program generates the following output.
 
search-method-output2 
 

6) Stack removeElementAt() method in Java

 
The Java.util.Stack.removeElementAt(int index) method is used to remove an element from a Stack from a specific position or index. In this process, the size of the Stack is automatically reduced by one and all other elements after the removed element is shifted downwards by one position.
 
This method accepts a mandatory parameter index of integer data type which specifies the position of the element to be removed from the Stack. This method does not return any value.
 
The complete program of Stack.removeElementAt() method example is listed below.
  1. import java.util.*;  
  2. public class RemoveElementAtExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<Integer> stack = new Stack<Integer>();  
  5.         stack.add(10);  
  6.         stack.add(20);  
  7.         stack.add(30);  
  8.         stack.add(40);  
  9.         stack.add(50);  
  10.         System.out.println("Stack: " + stack);  
  11.         stack.removeElementAt(0);  
  12.         System.out.println("Final Stack: " + stack);  
  13.     }  
The above program generates the following output.
 
remove-element-example-program 
 

7) Stack remove(int) method in Java

 
The Java.util.Stack.remove(int index) method is used to remove an element from a Stack from a specific position or index. This method accepts a mandatory parameter index is of integer data type and specifies the position of the element to be removed from the Stack. This method returns the element that has just been removed from the Stack.
 
The complete program of Stack.remove(int) method example is listed below.
  1. import java.util.*;  
  2. public class RemoveIntExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<Integer> stack = new Stack<Integer>();  
  5.         stack.add(10);  
  6.         stack.add(20);  
  7.         stack.add(30);  
  8.         stack.add(40);  
  9.         stack.add(50);  
  10.   
  11.         System.out.println("Stack: " + stack);  
  12.         int rem_ele = stack.remove(0);  
  13.         System.out.println("Removed element: " + rem_ele);  
  14.         System.out.println("Final Stack: " + stack);  
  15.     }  
The above program generates the following output.
 
remove-int-element-example 
 

8) Stack removeAllElements() method in Java

 
The Java.util.Stack.removeAllElements() method is used to removes all components from this Stack and sets its size to zero. The method does not take any argument and does not return any value.
 
The complete program of Stack.removeAllElements() method example is listed below.
  1. import java.util.*;  
  2. public class RemoveAllElementsExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<String> stack = new Stack<String>();  
  5.         stack.add("Welcome");  
  6.         stack.add("To");  
  7.         stack.add("C#Corner");  
  8.         stack.add("4");  
  9.         stack.add("Hello");  
  10.   
  11.         System.out.println("Stack: " + stack);  
  12.         stack.removeAllElements();  
  13.         System.out.println("The final Stack: " + stack);  
  14.     }  
The above program generates the following output.
 
remove-all-element-example 
 

9) Stack remove(Object) method in Java

 
The Java.util.Stack.remove(Object o) method is used to remove any particular element from the Stack. This method accepts a mandatory parameter o is of the object type of Stack and specifies the element to be removed from the Stack. Returns True if the specified element is found and removed from the Stack, else False.
 
The complete program of Stack.remove(Object) method example is listed below.
  1. import java.util.*;  
  2. public class RemoveObjectExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<String> stack = new Stack<String>();  
  5.         stack.add("Hello");  
  6.         stack.add("C#Corner");  
  7.         stack.add("10");  
  8.         stack.add("20");  
  9.   
  10.         System.out.println("Stack: " + stack);  
  11.         boolean res = stack.remove("20");  
  12.         System.out.println("Was 20 removed: " + res);  
  13.         System.out.println("Final Stack: " + stack);  
  14.     }  
The above program generates the following output.
 
remove-object-example-output 
 

10) Stack addAll(int, Collection) method in Java

 
The addAll(int, Collection) method of Stack Class is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a Stack. This function accepts two parameters Int index and Collection c.
 
index: This parameter is of integer data type and specifies the position in the Stack starting from where the elements from the container will be inserted. C: It is a collection of ArrayList. It is the collection whose elements are needed to be appended.
 
The method returns True if at least one action of append is performed, else False.
 
The complete program of Stack.addAll() method example is listed below.
  1. import java.util.*;  
  2. import java.util.ArrayList;  
  3. public class AddAllElementExample {  
  4.     public static void main(String args[]) {  
  5.         Stack<String> stack = new Stack<String>();  
  6.         stack.add("Hello");  
  7.         stack.add("programmers");  
  8.         stack.add("of");  
  9.         stack.add("C#corner");  
  10.         stack.add("20");  
  11.   
  12.         Collection<String> c = new ArrayList<String>();  
  13.         c.add("A");  
  14.         c.add("best");  
  15.         c.add("Place");  
  16.         c.add("for");  
  17.         c.add("beginners");  
  18.   
  19.         System.out.println("The Stack is: " + stack);  
  20.         stack.addAll(1, c);  
  21.         System.out.println("The new Stack is: " + stack);  
  22.     }  
The above program generates the following output.
 
add-all-method-example 
 

11) Stack listIterator() method in Java

 
The listIterator() method of Java.util.Stack class is used to return a list iterator over the elements in this stack (in proper sequence). The returned list iterator is fail-fast. This method returns a list iterator over the elements in this stack (in proper sequence).
 
The complete program of Stack.listIterator() method example is listed below.
  1. import java.util.*;  
  2.   
  3. public class ListLteratorExample {  
  4.     public static void main(String[] argv) throws Exception {  
  5.         try {  
  6.             Stack<String> stack = new Stack<String>();  
  7.             stack.add("A");  
  8.             stack.add("B");  
  9.             stack.add("C");  
  10.             stack.add("D");  
  11.   
  12.             System.out.println("Stack: " + stack);  
  13.             ListIterator<String> iterator = stack.listIterator();  
  14.             System.out.println("\nUsing ListIterator:\n");  
  15.             while (iterator.hasNext()) {  
  16.                 System.out.println("Value is : " + iterator.next());  
  17.             }  
  18.         } catch (NullPointerException e) {  
  19.             System.out.println("Exception thrown : " + e);  
  20.         }  
  21.     }  
The above program generates the following output.
 
list-iterator-example-output 
 

12) Stack listIterator(int) method in Java

 
The listIterator(int) method of Stack Class is used to return a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call to next. An initial call to previous would return the element with the specified index minus one. The returned list iterator is fail-fast.
 
This method takes the index of the first element as a parameter to be returned from the list iterator (by a call to next)This method takes the index of the first element as a parameter to be returned from the list iterator (by a call to next). This method throws IndexOutOfBoundsException if the index is out of range (index size()).
 
The complete program of Stack.listIterator(int) method example is listed below.
  1. import java.util.*;  
  2. public class ListItereatorIntExample {  
  3.     public static void main(String[] argv) throws Exception {  
  4.         try {  
  5.             Stack<String> stack = new Stack<String>();  
  6.             stack.add("A");  
  7.             stack.add("B");  
  8.             stack.add("C");  
  9.             stack.add("D");  
  10.   
  11.             System.out.println("Stack: " + stack);  
  12.             ListIterator<String> iterator = stack.listIterator(2);  
  13.             System.out.println("\nUsing ListIterator" + " from Index 2:\n");  
  14.             while (iterator.hasNext()) {  
  15.                 System.out.println("Value is : " + iterator.next());  
  16.             }  
  17.         } catch (IndexOutOfBoundsException e) {  
  18.             System.out.println("Exception thrown : " + e);  
  19.         }  
  20.     }  
The above program generates the following output.
 
list-iterator-int-example 
 

13) Stack trimToSize() method in Java

 
The trimToSize() method of Stack in Java trims the capacity of a Stack instance to be the list’s current capacity. This method is used to trim a Stack instance to the number of elements it contains. The method does not take any argument. It does not return any value. It trims the capacity of this Stack instance to the number of the element it contains.
 
The complete program of Stack.trimToSize() method example is listed below.
  1. import java.util.Stack;  
  2. public class TrimToSizeExample {  
  3.     public static void main(String[] args) {  
  4.         Stack<Integer> stack = new Stack<Integer>();  
  5.         stack.add(10);  
  6.         stack.add(20);  
  7.         stack.add(30);  
  8.         stack.add(40);  
  9.   
  10.         System.out.println("Stack: " + stack);  
  11.         System.out.println("Current capacity of Stack: " + stack.capacity());  
  12.         stack.ensureCapacity(15);  
  13.         System.out.println("New capacity of Stack: " + stack.capacity());  
  14.         stack.trimToSize();  
  15.         System.out.println("Current capacity of Stack" + " after use of trimToSize() method: " + stack.capacity());  
  16.     }  
The above program generates the following output.
 
trim-to-size-example 
 

14) Stack lastIndexOf(Object, int) method in Java

 
The Java.util.Stack.lastIndexOf(Object element, int last_index) method is used to the last index of the first occurrence of the specified element in this Stack, searching forwards from the last index, or returns -1 if the element is not found. More formally, returns the lowest last index i such that last index && Objects.equals(o, get(i))), or -1 if there is no such last index.
 
This method accepts two parameters:
element of the type of Stack. It specifies the element whose occurrence is needed to be checked in the Stack. last index of the type Integer. It specifies the last index to start searching from.
 
This method returns the last index or position of the first occurrence of the element in the Stack from the specified last index. Else it returns -1 if the element is not present in the Stack. The returned value is of integer type. This method throws IndexOutOfBoundsException if the specified index is greater than or equal to the current size of this vector.
 
The complete program od  Stack.lastIndexOf() method example is listed below.
  1. import java.util.*;  
  2. public class LastIndexOfExample {  
  3.     public static void main(String args[]) {  
  4.   
  5.         Stack<Integer> stack = new Stack<Integer>();  
  6.         stack.add(1);  
  7.         stack.add(2);  
  8.         stack.add(3);  
  9.         stack.add(10);  
  10.         stack.add(20);  
  11.   
  12.         System.out.println("Stack: " + stack);  
  13.         System.out.println("The 10th occurrence" + " of Geeks is at index: ");  
  14.         try {  
  15.             stack.lastIndexOf("Geeks"10);  
  16.         } catch (Exception e) {  
  17.             System.out.println(e);  
  18.         }  
  19.     }  
The above program generates the following output.
 
last-indexof-example 
 

15) Stack toString() method in Java

 
The toString() method of Java Stack is used to return a string representation of the elements of the Collection.
 
The String representation comprises a set representation of the elements of the Collection in the order they are picked by the iterator closed in square brackets[].This method is used mainly to display collections other than String type(for instance: Object, Integer)in a String Representation.
 
The complete program of Stack.toString() method example is listed below.
  1. import java.util.*;  
  2. public class ToStringMethodExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<Integer> stack = new Stack<Integer>();  
  5.         stack.add(10);  
  6.         stack.add(20);  
  7.         stack.add(30);  
  8.         stack.add(40);  
  9.   
  10.         System.out.println(stack.toString());  
  11.     }  
The above program generates the following output.
 
to-string-example-output 
 

16) Stack size() method in Java

 
The Java.util.Stack.size() method in Java is used to get the size of the Stack or the number of elements present in the Stack. The method does not take any argument. The method returns the size or the number of elements present in the Stack.
 
The complete program of Stack.size() method is listed below.
  1. import java.util.*;  
  2. public class SizeOfMethodExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<Integer> stack = new Stack<Integer>();  
  5.         stack.add(10);  
  6.         stack.add(15);  
  7.         stack.add(30);  
  8.         stack.add(20);  
  9.         stack.add(5);  
  10.   
  11.         System.out.println("Stack: " + stack);  
  12.   
  13.         System.out.println("The size is: " + stack.size());  
  14.     }  
The above program generates the following output.
 
size-of-method-example 
 

17) Stack capacity() method in Java

 
The capacity() method of Stack Class is used to get the capacity of this Stack. That is the number of elements present in this stack container. This function accepts a parameter E obj which is the object to be added at the end of the Stack. The method returns integer value which is the capacity of the Stack.
 
The complete program of Stack.capacity() method example is listed below.
  1. import java.util.*;  
  2. public class CapacityMethodExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<Integer> stack = new Stack<Integer>();  
  5.         stack.add(10);  
  6.         stack.add(20);  
  7.         stack.add(30);  
  8.         stack.add(40);  
  9.         stack.add(50);  
  10.   
  11.         System.out.println("The Stack is: " + stack);  
  12.   
  13.         System.out.println("Capacity: " + stack.capacity());  
  14.     }  
The above program generates the following output.
 
capacity-example-output 
 

18) Stack setElementAt() method in Java

 
The setElementAt() method of Java Stack is used to set the component at the specified index of this vector to be the specified object. The index must be a value greater than or equal to 0 and less than the current size of the vector.
 
This method throws ArrayIndexOutOfBoundsException if the index is out of range (index = size()) This method throws ArrayIndexOutOfBoundsException if the index is out of range (index = size()).
 
The complete program of Stack.setElementAt() method is listed below.
  1. import java.util.*;  
  2. public class SetElementAtExample {  
  3.     public static void main(String args[]) {  
  4.         Stack<String> stack = new Stack<String>();  
  5.         stack.add("Hello");  
  6.         stack.add("C#Corner");  
  7.         stack.add("5");  
  8.         stack.add("10");  
  9.         stack.add("20");  
  10.   
  11.         System.out.println("Stack:" + stack);  
  12.         stack.setElementAt("programmers"1);  
  13.         System.out.println("C#Corner replaced with programmers");  
  14.         System.out.println("The new Stack is:" + stack);  
  15.     }  
  16. }
The above program generates the following output.
 
set-element-at-example 
 

How does Stack push and pop work?

 
When a Stack is created it contains no elements. 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.
 
push-pop-in-stack 
 

Summary

 
In this article, we learned about Java Stack class, its constructor and methods and how to use them in the Java programming language.