java.util.Stack.pop() Method

Advertisements


Description

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.

Declaration

Following is the declaration for java.util.Stack.pop() method.

public Object pop()

Parameters

  • NA

Return Value

The method call returns the object at the top of this stack.

Exception

  • EmptyStackException--This is thrown if this stack is empty.

Example

The following example shows the usage of java.util.Stack.pop()

package com.tutorialspoint;

import java.util.*;

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

Let us compile and run the above program, this will produce the following result.

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



Advertisements
Advertisements