Java Stack pop() Method Example

This example demonstrates the usage of Stack.pop() method with an example.
Stack.pop() method is used to remove the object at the top of this stack and returns that object as the value of this function.

Java Stack pop() Method Example

Let's demonstrate the usage of a pop() method with an example:
package com.javaguides.collections.stackexamples;

import java.util.Stack;

public class StackMethodExample {

    public static void main(String[] args) {
        popMethod();
    }

    private static void popMethod() {
        // creating stack
        Stack < String > stack = new Stack < > ();

        // populating stack
        stack.push("Java");
        stack.push("JEE");
        stack.push("C");
        stack.push("C++");
        stack.push("Spring");
        stack.push("Hibernate");

        // removing top object
        System.out.println("Removed object is: " + stack.pop());

        // elements after remove
        System.out.println("Elements after remove: " + stack);
    }
}
Output:
Removed object is: Hibernate
Elements after remove: [Java, JEE, C, C++, Spring]

Reference

Comments