Java Stack add() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Inherited from Vector

Definition

In Java, a Stack inherits two add overloads. add(e) appends the element at the end of the underlying vector, which is the top of the stack, and returns true. add(index, e) inserts the element at a position and shifts the elements at and above that position up by one.

add(e) puts the element exactly where push(e) would; only the return value differs. add(index, e) does something push cannot: it slides an element under the top, for example at index 0, the bottom. That bends strict LIFO order, so use it on purpose.

An instance method inherited from java.util.Vector (since Java 1.2). Stack does not override it: index 0 is the bottom of the stack and index size() - 1 is the top.

Syntax

Pass just the element to append at the top, or an index and the element to insert.

Java
Stack<String> stack = new Stack<>();
stack.push("b");                 // [b]
boolean ok = stack.add("c");     // true, [b, c]  c is the top
stack.add(0, "a");               // [a, b, c]     a goes to the bottom
stack.add(3, "d");               // [a, b, c, d]  index == size() appends
stack.add(9, "x");               // throws ArrayIndexOutOfBoundsException
CallWhat it does
stack.add(e)Appends at the top; always returns true.
stack.add(0, e)Inserts at the bottom; every other element moves up one index.
stack.add(stack.size(), e)The same position as push(e).
stack.add(stack.size() - 1, e)Slides e just under the current top.

add(e) is constant time on average. add(index, e) shifts every element above index, so inserting near the bottom of a big stack costs more.

Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized boolean add(E e)
public void add(int index, E element)

// add(index, element) delegates to insertElementAt(element, index)

Parameters

ParameterDescription
e / element (E)The element to store. null and duplicates are allowed.
index (int)Only for add(index, element): where to insert, from 0 (bottom) to size() (new top).

Return Value

Return ValueDescription
booleanadd(e) always returns true (the stack changed).
voidadd(index, element) returns nothing.

Compare with Similar Methods

Other ways to put elements on a stack:

MethodHow it differs
push(e)Same position (the top); returns the element instead of true.
insertElementAt(e, i)Same as add(i, e) with the arguments swapped; returns nothing.
addAll(c)Appends a whole collection in one call.
ArrayList.add()The same two overloads on a list without synchronization.

Java Examples

Example 1 — Pile Books with add() and push()

Mix push and add on the same pile and compare what each call returns.

Java
import java.util.Stack;

public class StackAddAppend {
    public static void main(String[] args) {
        Stack<String> books = new Stack<>();
        books.push("Atlas");
        boolean added = books.add("Biology");
        String pushed = books.push("Chemistry");

        System.out.println("add returned:  " + added);
        System.out.println("push returned: " + pushed);
        System.out.println("pile: " + books);
        System.out.println("top: " + books.peek());
    }
}

How It Works

  1. Mix freely. push and add both append to the end of the vector, so Atlas, Biology, and Chemistry pile up in call order.
  2. Different return values. add returns true because it implements Collection.add; push returns the book itself.
  3. The last book added is the top. Chemistry is the rightmost element, and peek() returns it.

Example 2 — Slip Cards Under the Pile

Insert an ace at the bottom of a card pile and a 10 in the middle with add(index, e).

Java
import java.util.Stack;

public class StackAddAtIndex {
    public static void main(String[] args) {
        Stack<String> cards = new Stack<>();
        cards.push("J");
        cards.push("Q");
        cards.push("K");

        cards.add(0, "A");
        System.out.println("after add(0, A):  " + cards);
        cards.add(2, "10");
        System.out.println("after add(2, 10): " + cards);
        System.out.println("bottom " + cards.get(0) + ", top " + cards.peek());
    }
}

How It Works

  1. Index 0 is the bottom. add(0, "A") slides the ace under the whole pile, and J, Q, K each move up one index.
  2. Insert in the middle. add(2, "10") puts the 10 at index 2, between J and Q; Q and K shift to indexes 3 and 4.
  3. The top never moved. Neither insert touched the end of the vector, so peek() still returns K.

Example 3 — Which Indexes Does add() Accept?

Insert at size(), then try an index past the end of a stack of game levels.

Java
import java.util.Stack;

public class StackAddBounds {
    public static void main(String[] args) {
        Stack<Integer> levels = new Stack<>();
        levels.push(1);
        levels.push(2);

        levels.add(levels.size(), 3);
        System.out.println("add(size(), 3): " + levels + ", top " + levels.peek());
        try {
            levels.add(5, 9);
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("add(5, 9) failed: index 5 > size " + levels.size());
        }
        System.out.println("unchanged: " + levels);
    }
}

How It Works

  1. size() is a valid index. Inserting at index 2 in a two-element stack means "after the last element", so 3 becomes the new top.
  2. Beyond size() fails. Index 5 is greater than the size 3, so add(5, 9) throws ArrayIndexOutOfBoundsException; the printed text is the program's own.
  3. No partial change. The bounds check runs before anything moves, so the levels are still [1, 2, 3].

Frequently Asked Questions

add(e) puts the element in the same place, the top, but returns true. push(e) returns the element itself. Use push in stack code because it reads better.
It inserts the element at that index and shifts the elements above it up by one. Index 0 is the bottom of the stack.
Any value from 0 to size(). Passing size() appends at the top; a larger or negative index throws ArrayIndexOutOfBoundsException.
No. Vector.add always returns true, because a Stack accepts every element, including null and duplicates.
Usually you should not, because it breaks last-in, first-out order. It can help when repairing data, such as putting a forgotten item at the bottom.

Next: addAll()

Push every element of a collection in its iteration order.

addAll() 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.

12 people found this page helpful