The Wayback Machine - https://web.archive.org/web/20240618020300/https://www.geeksforgeeks.org/stack-push-method-in-java/
Open In App

Stack push() Method in Java

Last Updated : 10 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
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]


Previous Article
Next Article

Similar Reads

ArrayDeque push() Method in Java
The Java.util.ArrayDeque.push(E element) method is used to push an element into the Deque. The operation is similar to the operation in the stack. The element gets pushed onto the top of the deque. Syntax: Array_Deque.push(E element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to be pushed into the deque. R
2 min read
LinkedList push() Method in Java
The java.util.LinkedList.push() method is used to push an element at the starting(top) of the stack represented by LinkedList. This is similar to the addFirst() method of LinkedList and simply inserts the element at the first position or top of the linked list. Syntax: LinkedListObject.push(Object element) Parameters: The method accepts one paramet
2 min read
LinkedBlockingDeque push() method in Java
The push(E e) method of LinkedBlockingDeque pushes an element onto the stack represented by this deque. It inserts the element passed in the parameter to the front of the Deque if there is space. If the LinkedBlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. This function is similar to tha
2 min read
ConcurrentLinkedDeque push() method in Java with Examples
The push() method of ConcurrentLinkedDeque class is an in-built function in Java which pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is
2 min read
BlockingDeque push() method in Java with examples
The push(E e) method of BlockingDeque pushes an element onto the stack represented by this deque. It inserts the element passed in the parameter to the front of the Deque if there is space. If the BlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. This function is similar to that of addFirs
2 min read
How to Push Notification in Android using Firebase Cloud Messaging?
Firebase Cloud Messaging is a real-time solution for sending notifications to client apps without any kind of charges. FCM can reliably transfer notifications of up to 4Kb of payload. In this article, a sample app showing how this service can be availed is developed. Though FCM also allows sending out notifications using an app server, here Firebas
9 min read
Push Notifications in Android Using OneSignal
We have seen so many types of notifications that we received in many of the Android apps. These notifications inform our users about the new offers, new features, and many more inside our application. In this article, we will take a look at the implementation of the OneSignal notification platform in the Android app in Android Studio. We will be bu
6 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 one element is present, the index of the element cl
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 the stack is empty else it returns false. Below prog
4 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 the stack and then removes it.Exceptions: The meth
2 min read