Member-only story
Hi everyone, welcome back. In this tutorial, I will show how to use the built in functions regarding the stack in Java. A stack is a linear type of data structure that is capable of storing objects. The stack is a last-in-first-out or LIFO data structure which means that the last item placed into the stack is the first item that comes off of the stack. Say numbers of 1, 2, and 3 are added to the stack in that order. If items start to get removed from the stack, the first item to leave would be 3, then 2, then finally 1. The last item added into the stack is referred to as the top of the stack and the first item added is referred to as the bottom.
Stack
The Stack is already a class that is provided by Java, but it will have to be imported into your project using the java.util package.
import java.util.Stack;
The Stack class provided allows us to create an empty stack. Creating a stack is similar to creating other objects with Java.
Stack stack = new Stack();
Now let’s go over some of the built in functions that Java provides.
empty()
The first function is the empty() function which checks to see if the stack is empty or not. The empty() function returns a boolean.