The Wayback Machine - https://web.archive.org/web/20240618020307/https://www.geeksforgeeks.org/stack-pop-method-in-java/
Open In App

Stack pop() Method in Java

Last Updated : 12 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.
Syntax: 

STACK.pop()

Parameters: The method does not take any parameters.
Return Value: This method returns the element present at the top of the stack and then removes it.
Exceptions: The method throws EmptyStackException is thrown if the stack is empty.
Below programs illustrate the Java.util.Stack.pop() method: 
Program 1: 

Java




// Java code to illustrate pop()
import java.util.*;
 
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack<String> STACK = new Stack<String>();
 
        // Use add() method to add elements
        STACK.push("Welcome");
        STACK.push("To");
        STACK.push("Geeks");
        STACK.push("For");
        STACK.push("Geeks");
 
        // Displaying the Stack
        System.out.println("Initial Stack: " + STACK);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " +
                                         STACK.pop());
        System.out.println("Popped element: " +
                                         STACK.pop());
 
        // Displaying the Stack after pop operation
        System.out.println("Stack after pop operation "
                                             + STACK);
    }
}


Program 2: 

Java




// Java code to illustrate pop()
import java.util.*;
 
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack<Integer> STACK = new Stack<Integer>();
 
        // Use add() method to add elements
        STACK.push(10);
        STACK.push(15);
        STACK.push(30);
        STACK.push(20);
        STACK.push(5);
 
        // Displaying the Stack
        System.out.println("Initial Stack: " + STACK);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " +
                                         STACK.pop());
        System.out.println("Popped element: " +
                                         STACK.pop());
 
        // Displaying the Stack after pop operation
        System.out.println("Stack after pop operation "
                                             + STACK);
    }
}


Output: 

Initial Stack: [10, 15, 30, 20, 5]
Popped element: 5
Popped element: 20
Stack after pop operation [10, 15, 30]

 



Previous Article
Next Article

Similar Reads

ArrayDeque pop() Method in Java
The Java.util.ArrayDeque.pop() method in Java is used to pop an element from the deque. The element is popped from the top of the deque and is removed from the same. Syntax: Array_Deque.pop() Parameters: The method does not take any parameters. Return Value: This method returns the element present at the front of the Deque. Exceptions: The method t
2 min read
LinkedList pop() Method in Java
The java.util.LinkedList.pop() method is used to remove and return the top element from the stack represented by the LinkedList. The method simply pops out an element present at the top of the stack. This method is similar to removeFirst method in LinkedList. Syntax: LinkedListObject.pop() Parameters: The method does not take any parameter. Return
2 min read
LinkedBlockingDeque pop() method in Java
The pop() method of LinkedBlockingDeque pops an element from the stack represented by this deque. In other words, it removes and returns the first element of this deque. It returns null if the container is empty. Syntax: public E pop() Parameters: This method does not accept any parameters. Returns: This method returns the element at the front of t
2 min read
ConcurrentLinkedDeque pop() method in Java with Examples
The Java.util.ConcurrentLinkedDeque.pop() method in Java is used to pop an element from the ConcurrentLinkedDeque. The element is popped from the top of the ConcurrentLinkedDeque and is removed from the same.Syntax: ConcurrentLinkedDeque.pop() Parameters: The method does not take any parameters.Return Value: This method returns the element present
2 min read
How to Handle Self-Signed Certificate Pop-up in Selenium using Java?
While automating UI test cases we would have come across various challenges in our careers. Handling self-signed pop-ups is one of them. It’s a little bit tricky to handle such a pop-up as it’s not part of the browser, rather it’s a window pop-up. As we know, Selenium is not able to handle windows pop-ups directly. In this article, we will discuss
4 min read
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 cl
2 min read
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. Syntax: STACK.empty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if the stack is empty else it returns false. Below prog
4 min read
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. Syntax: STACK.peek() Parameters: The method does not take any parameters. Return Value: The method returns the element at the top
2 min read
Stack push() Method in Java
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. Syntax: STACK.push(E element)Parameters: The method accepts one parameter element of type Stack and refers to the element to be pushed into the stack. Return Value: The method returns the argument passed. It also acce
2 min read
Stack capacity() method in Java with Example
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. Syntax: public int capacity() Parameters: This function accepts a parameter E obj which is the object to be added at the end of the Stack. Return Value: The method returns integer value which is the capacit
2 min read