Java Stack size() Method

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

Definition

In Java, size() returns the number of elements currently on a Stack. Every push adds one, every pop removes one, and null elements count too.

The size is not the capacity, which is the length of the internal array that Vector manages (readable with capacity()). The size also gives you the index of the top element: size() - 1.

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

Syntax

Call size with no arguments.

Java
Stack<String> stack = new Stack<>();
int a = stack.size();       // 0
stack.push("x");
stack.push("y");
int b = stack.size();       // 2
int topIndex = b - 1;       // 1, the index of "y"
stack.pop();
int c = stack.size();       // 1
CallWhat it does
stack.size()The number of elements.
stack.size() - 1The index of the top element.
stack.size() == 0The same as empty() and isEmpty().
stack.capacity()Room in the internal array, not the element count.

size() is constant time: Vector keeps a running count of its elements.

Library declarations
Java
// java.util.Vector<E>, inherited by Stack<E>
public synchronized int size()

Parameters

ParameterDescription
Nonesize() takes no parameters.

Return Value

Return ValueDescription
intThe number of elements, 0 or more.

Compare with Similar Methods

Related counting methods:

MethodHow it differs
isEmpty()Only asks whether the size is 0.
empty()Stack's own zero check.
setSize(n)Changes the size, padding with null or truncating.
ArrayList.size()The same count on a list.

Java Examples

Example 1 — Watch the Size Grow and Shrink

Count a stack of cups before and after pushes and a pop.

Java
import java.util.Stack;

public class StackSizeDemo {
    public static void main(String[] args) {
        Stack<String> cups = new Stack<>();
        System.out.println("start: " + cups.size());
        cups.push("red");
        cups.push("blue");
        cups.push("green");
        System.out.println("after 3 pushes: " + cups.size());
        cups.pop();
        System.out.println("after 1 pop: " + cups.size() + ", top index " + (cups.size() - 1) + " = " + cups.get(cups.size() - 1));
    }
}

How It Works

  1. Starts at zero. A new stack holds no cups, so size() is 0.
  2. One per push, one per pop. Three pushes bring the count to 3; popping green brings it back to 2.
  3. The size points at the top. With two cups, the top is at index size() - 1 = 1, and get(1) confirms it is blue.

Example 2 — Size Is Not Capacity

Push eleven numbers and print both values after the tenth and the eleventh push.

Java
import java.util.Stack;

public class StackSizeVsCapacity {
    public static void main(String[] args) {
        Stack<Integer> numbers = new Stack<>();
        System.out.println("new:      size " + numbers.size() + ", capacity " + numbers.capacity());

        for (int i = 1; i <= 10; i++) {
            numbers.push(i);
        }
        System.out.println("10 items: size " + numbers.size() + ", capacity " + numbers.capacity());

        numbers.push(11);
        System.out.println("11 items: size " + numbers.size() + ", capacity " + numbers.capacity());
    }
}

How It Works

  1. Room for ten. new Stack<>() calls Vector's no-argument constructor, documented to start with an internal array of 10 slots, while the size is 0.
  2. Full but not grown. After ten pushes, size and capacity are both 10: every slot is in use.
  3. Doubling on overflow. The eleventh push needs one more slot, so the array grows to 20 (a capacity increment of 0 means "double"), while the size is simply 11.

Example 3 — Keep at Most Three Undo Steps

Use size() to cap an undo history and drop the oldest action when it gets too long.

Java
import java.util.Stack;

public class StackSizeLimit {
    static final int MAX = 3;

    public static void main(String[] args) {
        Stack<String> undo = new Stack<>();
        String[] actions = {"cut", "paste", "bold", "italic", "underline"};

        for (String a : actions) {
            undo.push(a);
            if (undo.size() > MAX) {
                String dropped = undo.remove(0);
                System.out.println("dropped oldest: " + dropped);
            }
        }
        System.out.println("kept " + undo.size() + ": " + undo);
    }
}

How It Works

  1. Push, then measure. After every push the loop compares size() with the limit of 3.
  2. Trim from the bottom. When a fourth action arrives, remove(0) drops the oldest one: first cut, then paste.
  3. A bounded history. The stack never keeps more than three actions, and the newest, underline, stays on top for the next undo.

Frequently Asked Questions

The number of elements currently on the stack. It is 0 for an empty stack.
size() counts elements. capacity() is the length of the internal array, which is at least the size and grows automatically.
Yes. A pushed null is an element like any other.
Use size() - 1. Index 0 is the bottom.
No. Arrays have length; collections, including Stack, use the size() method.

Next: toArray()

Copy the elements, bottom to top, into an array.

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

13 people found this page helpful