Java Stack Implementation using Array

This tutorial gives example of implementing a Stack data structure using Array. The stack offers to put new object on the stack (method push()) and to get objects from the stack (method pop()). A stack returns the object according to last-in-first-out (LIFO). Please note that JDK provides a default java stack implementation as class java.util.Stack.

The two mandatory operations applicable to all stack implementations are:

push() : In which a data item is placed at the location pointed to by the stack pointer.

pop() : In which a data item is removed and returned from the location pointed to by the stack pointer.

Some environments that rely heavily on stacks may provide additional operations as per requirements. Most common uses of Stack are expression evaluation and syntax parsing. You can find them in undo\redo operations in word processors as well.

Java Stack Implementation Sourcecode

Below program is sample implementation of Stack data structure. Feel free to modify the sourcecode as per your need.