Stack push() Method in Java Last Updated : 10 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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. Syntax: STACK.push(E element)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. It also accepts the null value unlike ArrayDeque.push() which throws java.lang.NullPointerException on doing the same. Below programs illustrate the Java.util.Stack.push() method: Program 1: Adding String elements into the Stack. Java // Java Code to illustrate push() Method import java.util.*; // Main class public class StackDemo { // Main driver method public static void main(String args[]) { // Creating an empty Stack Stack& lt; String& gt; STACK = new Stack& lt; String& gt; (); // Adding elements into the stack // using push() method STACK.push(" Welcome & quot;); STACK.push(" To & quot;); STACK.push(" Geeks & quot;); STACK.push(" For & quot;); STACK.push(" Geeks & quot;); // Displaying the Stack System.out.println(" Initial Stack : " + STACK); // Pushing elements into the stack STACK.push(" Hello & quot;); STACK.push(" World & quot;); // Displaying the final Stack System.out.println(" Final Stack : " + STACK); } } Output:Initial Stack: [Welcome, To, Geeks, For, Geeks] Final Stack: [Welcome, To, Geeks, For, Geeks, Hello, World] Program 2: Adding Integer elements into the Stack. Java // Java code to illustrate push() method import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<Integer> STACK = new Stack<Integer>(); // Use push() to add elements into the Stack STACK.push(10); STACK.push(15); STACK.push(30); STACK.push(20); STACK.push(5); STACK.push(null); // Displaying the Stack System.out.println("Initial Stack: " + STACK); // Pushing elements into the Stack STACK.push(1254); STACK.push(4521); // Displaying the final Stack System.out.println("Final Stack: " + STACK); } } Output:Initial Stack: [10, 15, 30, 20, 5] Final Stack: [10, 15, 30, 20, 5, 1254, 4521] Comment More infoAdvertise with us Next Article Stack pop() Method in Java chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-Stack +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Stack Class in Java The Java Collection framework provides a Stack class, which implements a Stack data structure. The class is based on the basic principle of LIFO (last-in-first-out). Besides the basic push and pop operations, the class also provides three more functions, such as empty, search, and peek. The Stack cl 11 min read Stack push() Method in Java 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. Syntax: STACK.push(E element)Parameters: The method accepts one parameter element of type Stack and refers to the element to be pushed into the stack. Return Value: Th 2 min read Stack pop() Method in Java The Java.util.Stack.pop() method in Java is used to pop an element from the stack. The element is popped from the top of the stack and is removed from the same.Syntax: STACK.pop() Parameters: The method does not take any parameters.Return Value: This method returns the element present at the top of 2 min read Stack peek() Method in Java The java.util.Stack.peek() method in Java is used to retrieve or fetch the first element of the Stack or the element present at the top of the Stack. The element retrieved does not get deleted or removed from the Stack. Syntax:STACK.peek()Parameters: The method does not take any parameters. Return V 2 min read Stack empty() Method in Java The java.util.Stack.empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false. Syntax: STACK.empty() Parameters: The method does not take any parameters. Return Value: The method returns boolean true if th 4 min read Stack search() Method in Java The java.util.Stack.search(Object element) method in Java is used to search for an element in the stack and get its distance from the top. This method starts the count of the position from 1 and not from 0. The element that is on the top of the stack is considered to be at position 1. If more than o 2 min read Stack removeElementAt() method in Java with Example The Java.util.Stack.removeElementAt(int index) method is used to remove an element from a Stack from a specific position or index. In this process the size of the Stack is automatically reduced by one and all other elements after the removed element are shifted downwards by one position. Syntax: Sta 2 min read Stack remove(int) method in Java with Example The Java.util.Stack.remove(int index) method is used to remove an element from a Stack from a specific position or index. Syntax: Stack.remove(int index) Parameters: This method accepts a mandatory parameter index is of integer data type and specifies the position of the element to be removed from t 2 min read Stack removeAllElements() method in Java with Example The Java.util.Stack.removeAllElements() method is used to removes all components from this Stack and sets its size to zero. Syntax: Stack.removeAllElements() Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Java.u 2 min read Stack remove(Object) method in Java with Example The Java.util.Stack.remove(Object o) method is used to remove any particular element from the Stack. Syntax: Stack.remove(Object o) Parameters: This method accepts a mandatory parameter o is of the object type of Stack and specifies the element to be removed from the Stack. Return Value: Returns Tru 2 min read Like