The push() method in Java is used to add an element to the top of a Stack. It is commonly used to insert elements while maintaining the LIFO (Last-In, First-Out) order.
- It accepts one element as a parameter and returns the pushed element.
- It allows
nullelements to be added to theStack.
import java.util.Stack;
public class GFG{
public static void main(String[] args){
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println(stack);
}
}
Output
[10, 20, 30]
Explanation: The push() method adds each element to the top of the stack. Here, 30 is the top element because it was added last.
Syntax
public E push(E item);
Here, E represents the type of element stored in the Stack, and item is the element to be added.
- Parameter: item -> The element to be added to the top of the stack.
- Return Value: Returns the element that was pushed onto the stack.
Note:
Stack.push()allowsnullelements. In contrast,ArrayDeque.push()throws aNullPointerExceptionwhennullis passed.
Examples on push() Method
The following examples demonstrate how the push() method can be used to add different types of elements to a Stack.
Example: Adding String Elements to a Stack
The following program demonstrates how to add string elements to a Stack using the push() method.
import java.util.Stack;
public class StackDemo {
public static void main(String[] args) {
// Creating an empty Stack
Stack<String> stack = new Stack<>();
// Adding elements using push()
stack.push("Welcome");
stack.push("To");
stack.push("Geeks");
stack.push("For");
stack.push("Geeks");
// Displaying the initial Stack
System.out.println("Initial Stack: " + stack);
// Adding more elements using push()
stack.push("Hello");
stack.push("World");
// 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]
Explanation:
- The program creates an empty Stack<String> named stack.
- The push() method adds "Welcome", "To", "Geeks", "For", and "Geeks" to the stack.
- The initial stack is displayed using System.out.println().
- "Hello" and "World" are then added to the top of the stack using push().
- Finally, the updated stack is displayed, showing all elements in the order they were added.
Example: Adding Integer Elements to a Stack
The following program demonstrates how to add integer elements to a Stack, including a null value.
import java.util.Stack;
public class StackDemo {
public static void main(String[] args) {
// Creating an empty Stack
Stack<Integer> stack = new Stack<>();
// Adding elements to the Stack using push()
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 additional 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, null] Final Stack: [10, 15, 30, 20, 5, null, 1254, 4521]
Explanation:
- The program creates an empty Stack<Integer> named stack.
- The push() method adds integer values 10, 15, 30, 20, 5, and null to the stack.
- The initial stack is displayed using System.out.println().
- The values 1254 and 4521 are then added to the top of the stack using push().
- Finally, the updated stack is displayed, including the newly added elements.