Java Stack get() Method

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

Definition

In Java, get(index) reads the element at a position in a Stack without removing it. Index 0 is the bottom (the first element pushed) and size() - 1 is the top.

get comes from the List interface through Vector, so it lets you look anywhere in the stack, not only at the top. That is handy for printing or debugging a stack, while stack algorithms themselves usually need only peek().

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 the index to read, counted from the bottom.

Java
Stack<String> stack = new Stack<>();
stack.push("first");
stack.push("second");
stack.push("third");                         // [first, second, third]
String bottom = stack.get(0);                // "first"
String top = stack.get(stack.size() - 1);    // "third", same as peek()
stack.get(3);                                // throws ArrayIndexOutOfBoundsException
CallWhat it does
stack.get(0)The bottom element.
stack.get(stack.size() - 1)The top element, like peek().
stack.get(stack.size() - 2)The element just under the top.
stack.elementAt(i)The legacy Vector name for the same read.

get is a constant-time array read, so looping over every index is cheap.

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

Parameters

ParameterDescription
index (int)Position to read, from 0 (bottom) to size() - 1 (top).

Return Value

Return ValueDescription
EThe element at that index; the stack is unchanged.

Compare with Similar Methods

Other ways to read a stack:

MethodHow it differs
elementAt(i)The same read under its Java 1.0 name.
peek()Reads only the top; throws EmptyStackException when empty.
firstElement()Reads index 0 without an index argument.
ArrayList.get()The same call on a list, where index 0 is simply the first element.

Java Examples

Example 1 — Read the Bottom, the Top, and Below the Top

Read three positions of a pile of plates and compare one of them with peek().

Java
import java.util.Stack;

public class StackGetDemo {
    public static void main(String[] args) {
        Stack<String> plates = new Stack<>();
        plates.push("white");
        plates.push("blue");
        plates.push("green");
        plates.push("red");

        System.out.println("bottom:       " + plates.get(0));
        System.out.println("top:          " + plates.get(plates.size() - 1));
        System.out.println("under top:    " + plates.get(plates.size() - 2));
        System.out.println("same as peek: " + plates.get(3).equals(plates.peek()));
    }
}

How It Works

  1. Index 0 is the first plate. white was pushed first, so it is at the bottom and get(0) returns it.
  2. Count back from the size. With four plates, index 3 is the top (red) and index 2 is the plate right under it (green).
  3. Nothing is removed. The comparison with peek() is true, and all four plates are still on the stack after the reads.

Example 2 — Print a Call Stack, Most Recent First

Show the frames of a simulated call stack the way a stack trace does, without popping anything.

Java
import java.util.Stack;

public class StackGetTopDown {
    public static void main(String[] args) {
        Stack<String> frames = new Stack<>();
        frames.push("main");
        frames.push("loadConfig");
        frames.push("readFile");
        frames.push("parseLine");

        System.out.println("Call stack (most recent first):");
        for (int i = frames.size() - 1; i >= 0; i--) {
            System.out.println("  at " + frames.get(i));
        }
        System.out.println("depth: " + frames.size());
    }
}

How It Works

  1. Frames pile up. Each method call pushes its name, so the method running right now, parseLine, is at the top.
  2. Loop downward. Starting at size() - 1 and counting to 0 visits the frames from top to bottom, the way an exception stack trace lists them.
  3. Look without popping. get(i) only reads, so the depth is still 4 after printing; popping inside the loop would have destroyed the stack.

Example 3 — Check the Index Before get()

A helper validates floor numbers in a two-floor building before reading them.

Java
import java.util.Stack;

public class StackGetBounds {
    static String describe(Stack<String> s, int i) {
        if (i >= 0 && i < s.size()) {
            return "index " + i + " = " + s.get(i);
        }
        return "index " + i + " is out of range";
    }

    public static void main(String[] args) {
        Stack<String> floors = new Stack<>();
        floors.push("lobby");
        floors.push("office");

        System.out.println(describe(floors, 1));
        System.out.println(describe(floors, 2));
        System.out.println(describe(floors, -1));
        try {
            floors.get(2);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("unchecked get(2) threw an index exception");
        }
    }
}

How It Works

  1. The valid range is 0 to size() - 1. Two floors means only indexes 0 and 1 exist, so describe reads office at index 1.
  2. Guard both ends. The helper rejects 2 (too high) and -1 (negative) before calling get, so neither causes an exception.
  3. Unchecked call. floors.get(2) throws ArrayIndexOutOfBoundsException, caught here through its parent type IndexOutOfBoundsException.

Frequently Asked Questions

The bottom element, the one pushed first. The top is get(size() - 1).
No. It only reads. Use pop() to remove the top or remove(index) to remove by position.
ArrayIndexOutOfBoundsException, which is a subclass of IndexOutOfBoundsException. Catch IndexOutOfBoundsException to handle both.
None in behavior. get(index) comes from the List interface; elementAt(index) is the original Vector name from Java 1.0.
It is fine for printing or debugging. If your code needs random access often, a List expresses that intent better than a stack.

Next: indexOf()

Position of the first match, counted from the bottom.

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

9 people found this page helpful