In Java, push(item) puts an element on the top of a Stack. The element pushed last is the first one that pop() removes and peek() shows: last in, first out.
push also returns the element you pushed, which is handy inside an expression. Internally it calls Vector's addElement, so the new element goes to the end of the internal array. That is why System.out.println(stack) shows the top on the right.
Declared in java.util.Stack itself (since Java 1.0). Stack<E> extends Vector<E> and keeps the top at the end of the vector, so printing a stack lists it from bottom to top.
Stack and ArrayDeque print in opposite orders.
ArrayDeque.push(e) adds at the front of the deque, so pushing 10, 20, 30 prints [30, 20, 10] with the top first. A Stack prints [10, 20, 30] with the top last. Both return 30 from peek().
Syntax
Pass the element to put on top. The return value is that same element.
Java
Stack<String> stack = new Stack<>();
stack.push("a"); // [a]
stack.push("b"); // [a, b] b is the top
String same = stack.push("c"); // same is "c", stack is [a, b, c]
Call
What it does
stack.push(x)
Puts x on top; the size grows by one.
T t = stack.push(x)
Same, and keeps a reference to x (the return value).
stack.add(x)
Also appends at the top, but returns true (from List).
stack.addElement(x)
Vector's legacy version: appends at the top and returns nothing.
push runs in constant time on average. When the internal array is full, Vector copies it into one twice as large (its default growth policy), an occasional hidden cost.
Library declarations
Java
// java.util.Stack<E>
public E push(E item)
// body: addElement(item); return item;
Parameters
Parameter
Description
item (E)
The element to put on top. It can be null and can equal elements already on the stack.
Adds at the front of a deque, rejects null, and prints the top first.
Java Examples
Example 1 — Record Editor Actions for Undo
An editor pushes every action so the newest one can be undone first.
Java
import java.util.Stack;
public class StackPushUndo {
public static void main(String[] args) {
Stack<String> undo = new Stack<>();
undo.push("type Hello");
undo.push("make bold");
undo.push("delete word");
String last = undo.push("insert image");
System.out.println("pushed: " + last);
System.out.println("undo stack: " + undo);
System.out.println("size: " + undo.size());
System.out.println("next undo: " + undo.peek());
}
}
Output
pushed: insert image
undo stack: [type Hello, make bold, delete word, insert image]
size: 4
next undo: insert image
How It Works
Record each action. The editor pushes a short description of every change, oldest first, so the newest action always lands on top.
The return value is the argument.undo.push("insert image") hands back "insert image", which is stored in last and printed.
Top printed last. The list prints oldest to newest, and peek() confirms that "insert image", the fourth action, is the one an Undo click reverses first.
Example 2 — Same Pushes, Opposite Print Order
Push the same three numbers onto a Stack and an ArrayDeque, then print both.
Java
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
public class StackPushVsDeque {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
Deque<Integer> deque = new ArrayDeque<>();
for (int i = 1; i <= 3; i++) {
stack.push(i * 10);
deque.push(i * 10);
}
System.out.println("Stack: " + stack);
System.out.println("ArrayDeque: " + deque);
System.out.println("tops: " + stack.peek() + " and " + deque.peek());
}
}