The Java.util.Stack.push(E element) method is used to push an element into the Stack. The element gets pushed onto the top of the Stack.
Parameters: The method accepts one parameter element of type Stack and refers to the element to be pushed into the stack.
Return Value: The method returns the argument passed.
Below programs illustrate the Java.util.Stack.push() method:
Program 1: Adding String elements into the Stack.
Initial Stack: [Welcome, To, Geeks, For, Geeks] Final Stack: [Welcome, To, Geeks, For, Geeks, Hello, World]
Program 2: Adding Integer elements into the Stack.
Initial Stack: [10, 15, 30, 20, 5] Final Stack: [10, 15, 30, 20, 5, 1254, 4521]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.