Java Stack firstElement() Method

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

Definition

In Java, firstElement() returns the element at index 0 of a Stack: the bottom of the stack, which is the oldest element still stored. Nothing is removed.

The name can mislead on a stack: "first" means first in the vector, not the next element to come off. The next one to come off is the top, which peek() and lastElement() return.

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 it with no arguments to read index 0.

Java
Stack<String> stack = new Stack<>();
stack.push("oldest");
stack.push("middle");
stack.push("newest");                    // [oldest, middle, newest]
String bottom = stack.firstElement();    // "oldest"
String top = stack.peek();               // "newest"
new Stack<String>().firstElement();      // throws NoSuchElementException
CallWhat it does
stack.firstElement()The bottom element (index 0).
stack.get(0)The same element; an index exception when empty.
stack.lastElement()The other end: the top.
stack.isEmpty() ? null : stack.firstElement()A read that gives null for an empty stack.

firstElement is a constant-time read of index 0.

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

Parameters

ParameterDescription
NonefirstElement() takes no parameters.

Return Value

Return ValueDescription
EThe element at index 0, the bottom of the stack.

Compare with Similar Methods

Reading either end of a stack:

MethodHow it differs
lastElement()Reads the top instead of the bottom.
get(0)Same element; throws an index exception when empty.
peek()Reads the top; throws EmptyStackException when empty.
remove(0)Reads and removes the bottom element.

Java Examples

Example 1 — Find the Entry Point of a Call Stack

The bottom of a call stack is the method that started everything.

Java
import java.util.Stack;

public class StackFirstElementDemo {
    public static void main(String[] args) {
        Stack<String> calls = new Stack<>();
        calls.push("main");
        calls.push("startServer");
        calls.push("handleRequest");

        System.out.println("entry point: " + calls.firstElement());
        System.out.println("running now: " + calls.peek());
        System.out.println("stack: " + calls);
    }
}

How It Works

  1. The first push stays at the bottom. main was called first, so it is index 0 and firstElement() names it as the entry point.
  2. Opposite ends. peek() returns handleRequest, the most recent call, from the other end of the same stack.
  3. A read-only look. Printing the stack afterwards shows all three calls in place, from main up to handleRequest.

Example 2 — Close Game Screens Back to the Main Menu

A game stacks its screens; closing them one by one never disturbs the bottom screen.

Java
import java.util.Stack;

public class StackFirstElementAfterPops {
    public static void main(String[] args) {
        Stack<String> screens = new Stack<>();
        screens.push("main menu");
        screens.push("level select");
        screens.push("level 3");
        screens.push("pause menu");

        while (screens.size() > 1) {
            System.out.println("close " + screens.pop() + ", bottom is still " + screens.firstElement());
        }
        System.out.println("back at: " + screens.peek());
    }
}

How It Works

  1. Screens stack up. The game pushes each screen it opens, so the main menu is at the bottom and the pause menu on top.
  2. Pops never reach index 0 here. The loop stops while one screen remains, and every pass shows that firstElement() is unaffected by popping the top.
  3. Bottom and top meet. With one screen left, peek() and firstElement() return the same element: the main menu.

Example 3 — Three Different Failures on an Empty Stack

Read the bottom, the top, and index 0 of an empty stack and see which exception each call throws.

Java
import java.util.EmptyStackException;
import java.util.NoSuchElementException;
import java.util.Stack;

public class StackFirstElementEmpty {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        try {
            stack.firstElement();
        } catch (NoSuchElementException e) {
            System.out.println("firstElement(): " + e.getClass().getSimpleName());
        }
        try {
            stack.peek();
        } catch (EmptyStackException e) {
            System.out.println("peek():         " + e.getClass().getSimpleName());
        }
        try {
            stack.get(0);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("get(0):         an index exception");
        }
    }
}

How It Works

  1. Vector's exception. firstElement() on an empty stack throws NoSuchElementException from java.util, which needs its own import.
  2. Stack's exception. peek() reports the same situation with EmptyStackException, a separate class, so a catch for one does not catch the other.
  3. The List way. get(0) fails with an index exception instead; the program prints fixed text so the output does not depend on the exact subclass or message.

Frequently Asked Questions

The bottom element at index 0, which is the oldest element still on the stack. It is not the next element pop() would return.
firstElement() reads the bottom; peek() reads the top. On an empty stack firstElement() throws NoSuchElementException while peek() throws EmptyStackException.
java.util.NoSuchElementException.
They return the same element. On an empty stack, get(0) throws ArrayIndexOutOfBoundsException instead of NoSuchElementException.
No. To remove the bottom element use remove(0) or removeElementAt(0).

Next: get()

Read the element at an index; index 0 is the bottom.

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

8 people found this page helpful