java how to pop an element and add it to the output strinf code example

Example 1: add element to stack java

Stack<String> stack = new Stack<String>();   
stack.add("Geeks");

Example 2: java 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);
   }    
}

Tags:

Java Example