What does the method pop() do in java?


The pop() method is used to remove the object at the top of this stack and returns that object as the value of this function.

Example

import java.util.*;

public class StackDemo {
   public static void main(String args[]) {
      Stack st = new Stack();
      st.push("Java");
      st.push("Source");
      st.push("code");
      System.out.println("Removed object is: "+st.pop());
      System.out.println("Elements after remove: "+st);
   }
}

Output

Removed object is: code
Elements after remove: [Java, Source]

Updated on: 30-Jul-2019

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements