What does the method push(Object item) do in java?



The push(Object item) method is used to Pushes an item onto the top of this stack.

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("Elements in the stack: "+st);
   }
}

Output

Elements in the stack: [Java, Source, code]

Advertisements