Java Collection Tutorial - Java Stack.push(E item)








Syntax

Stack.push(E item) has the following syntax.

public E push(E item)

Example

In the following code shows how to use Stack.push(E item) method.

//from   ww  w .  ja  v a 2 s .co  m


import java.util.Stack;

public class Main {
   public static void main(String args[]) {
      
      Stack st = new Stack();
      
      st.push("Java");
      st.push("Source");
      st.push("from java2s.com");

      // checking elements
      System.out.println("Elements in the stack: "+st);
   }     
}

The code above generates the following result.