Home Java Stack Methods get() Java Stack get() Method 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.
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 Call What 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.util.Vector<E>, inherited by Stack<E>
public synchronized E get(int index) Parameters Parameter Description index (int)Position to read, from 0 (bottom) to size() - 1 (top).
Return Value Return Value Description EThe element at that index; the stack is unchanged.
Compare with Similar Methods Other ways to read a stack:
Method How 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 1. Bottom and top 2. Print top-down 3. Check the index 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().
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()));
}
} bottom: white
top: red
under top: green
same as peek: true How It Works Index 0 is the first plate. white was pushed first, so it is at the bottom and get(0) returns it.Count back from the size. With four plates, index 3 is the top (red) and index 2 is the plate right under it (green).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.
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());
}
} Call stack (most recent first):
at parseLine
at readFile
at loadConfig
at main
depth: 4 How It Works Frames pile up. Each method call pushes its name, so the method running right now, parseLine, is at the top.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.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.
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");
}
}
} index 1 = office
index 2 is out of range
index -1 is out of range
unchecked get(2) threw an index exception How It Works The valid range is 0 to size() - 1. Two floors means only indexes 0 and 1 exist, so describe reads office at index 1.Guard both ends. The helper rejects 2 (too high) and -1 (negative) before calling get, so neither causes an exception.Unchecked call. floors.get(2) throws ArrayIndexOutOfBoundsException, caught here through its parent type IndexOutOfBoundsException.Frequently Asked Questions Which element does get(0) return on a Java Stack? The bottom element, the one pushed first. The top is get(size() - 1).
Does get() remove the element from the Stack? No. It only reads. Use pop() to remove the top or remove(index) to remove by position.
What exception does Stack.get() throw for a bad index? ArrayIndexOutOfBoundsException, which is a subclass of IndexOutOfBoundsException. Catch IndexOutOfBoundsException to handle both.
What is the difference between get() and elementAt()? None in behavior. get(index) comes from the List interface; elementAt(index) is the original Vector name from Java 1.0.
Is using get() on a Stack bad practice? It is fine for printing or debugging. If your code needs random access often, a List expresses that intent better than a stack.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion