Java Stack push() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Stack method

Definition

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]
CallWhat 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

ParameterDescription
item (E)The element to put on top. It can be null and can equal elements already on the stack.

Return Value

Return ValueDescription
EThe item argument itself, not the previous top.

Compare with Similar Methods

Other ways to put an element on top:

MethodHow it differs
add(e)Same position (the top) but returns true; add(index, e) can also insert lower down.
insertElementAt(e, i)Inserts at any index; with i = size() it acts like push.
addAll(c)Pushes a whole collection; its last element ends on top.
ArrayDeque.push(e)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());
    }
}

How It Works

  1. Record each action. The editor pushes a short description of every change, oldest first, so the newest action always lands on top.
  2. The return value is the argument. undo.push("insert image") hands back "insert image", which is stored in last and printed.
  3. 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());
    }
}

How It Works

  1. Identical pushes. Both collections receive 10, 20, and 30 in that order; each int is autoboxed to an Integer.
  2. Different storage. Stack.push appends to the end of its Vector, while ArrayDeque.push inserts at the front, so the printed lists are mirror images.
  3. Same top. The print order is only a display detail: peek() returns 30, the last value pushed, on both.

Example 3 — Push Duplicates and null

Track nested HTML tags: the same tag can be open twice, and a Stack even accepts null.

Java
import java.util.Stack;

public class StackPushNullsDuplicates {
    public static void main(String[] args) {
        Stack<String> tags = new Stack<>();
        tags.push("html");
        tags.push("div");
        tags.push("div");
        tags.push(null);

        System.out.println(tags + " size=" + tags.size());
        System.out.println("top is null: " + (tags.peek() == null));
        System.out.println("div is " + tags.search("div") + " from the top");
    }
}

How It Works

  1. Duplicates are fine. Two nested div tags are pushed one after the other; a stack never checks for equal values.
  2. null is a valid element. push(null) succeeds on a Stack, prints as null, and becomes the top that peek() returns.
  3. Nearest copy wins. search("div") reports the div closest to the top, one position below the null, so the distance is 2.

Frequently Asked Questions

It returns the element you passed in. stack.push("x") returns "x", not the previous top and not a boolean.
Both put the element on top. push() returns the element and is declared in Stack; add() returns true and comes from the List interface through Vector.
Yes. Stack allows null elements. ArrayDeque does not: its push(null) throws NullPointerException.
Stack keeps the top at the end of its Vector, and toString prints in index order from bottom to top. The rightmost element is the top.
Not in normal use. The stack grows automatically; only running out of memory stops it.

Next: search()

How far an element is from the top (top = 1), or -1.

search() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful