Java Stack push() Method Example

This example demonstrates the usage of Stack.push() method with an example.
Stack class push() method pushes an item onto the top of this stack.

Java Stack push() Method Example

Let's demonstrate the usage of push() method with an example:
package com.javaguides.collections.stackexamples; import java.util.Stack; public class StackMethodExample { public static void main(String[] args) { pushMethod(); } private static void pushMethod() { // creating stack Stack < String > stack = new Stack < > (); // populating stack stack.push("Java"); stack.push("JEE"); stack.push("C"); stack.push("C++"); stack.push("Spring"); stack.push("Hibernate"); // checking elements System.out.println("Elements in the stack: " + stack); } }
Output:
Elements in the stack: [Java, JEE, C, C++, Spring, Hibernate]

Reference


Comments