Member-only story

Java Programming Language: Stack

Jesse L
3 min readApr 13, 2022

--

Hi everyone, welcome back. In this tutorial, we will be going over the Stack and how to use the built in functions regarding them. 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. With this introduction out of the way, let’s get into it.

Creating the Stack

Let’s start by creating a Stack. Before we can create the Stack, we need to make sure we have our Stack import, which is included within the java.util package:

import java.util.Stack;

Now, we can successfully create our Stack:

Stack<Integer> myStack = new Stack<Integer>();

So now, we have created our Stack variable of the ‘Integer’ data type and named it ‘myStack.’ When we create our Stack, we need to specify the data type. As we can see in the example above, we have used the ‘Integer’ data type. We can use other data types as well, such as the ‘String’ data type.

Adding Elements Into Our Stack

Now, let’s try to add elements into our stack. We can do this by using the built in push() function that is provided. Let’s see an example:

--

--

Jesse L
Jesse L

Written by Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.

Responses (1)