Understand Java Stack Class With Example | Detailed Guide | 2023

In this article, We will come to know about Java Stack and its in-built methods in Java.

A stack is a linear data structure used to store objects. It is a part of the Java collection framework. It is based on the LIFO(Last-in-first-out) format.

Stack class can also be said to extend the Vector class. We will discuss some in-built functions soon. till then, Let us take a quick introduction to Stack.

Java Stack Class
Stack Class In Java

The meaning of “Stack” suggests itself. It means usually a pile. As we can see in the above image Integer type objects are stacked over each other and create a pile. It works on the LIFO(Last-in-first-out) format, Which means, The last element inserted into the stack will come out first.

A Top counter counts the number of elements present in the Stack. In the image above top counter is at 3(0-based indexing). The initial value of the top is generally -1, Which means the Stack is empty. Now, Let us move toward the declaration of Stack in Java.

Declaration of Stack

For the declaration of Stack, We use Stack() constructor. The syntax we follow for the declaration is as:

Stack<E> Stk = new Stack<E>();

Where “Stk” is the identifier and “E” is the type of object needed to store in Stack. It is a must to import java.util.Stack for declaring a Stack. The code for the declaration of Stack is provided below as a demonstration.

Code:

import java.util.Stack;
public class Main
{
	public static void main(String[] args) {
	Stack<Integer> Stk = new Stack();
	System.out.println(Stk);
	}
}

Output:

The output of the Declaration Code
The output of the Declaration Code

In the above output, We can see opening and closing square braces having nothing in between them, Because the stack is empty, Formally, no element is inserted in Stack.

Now, Let us move toward the in-built methods of Stack in Java.

Methods of Stack

Java’s Stack class provides a rich variety of in-built functions for performing insertion, deletion, and for other tasks. Let us see the in-built methods one by one along with the code and their output.

Push Methods in Java Stack class

Push() method: For inserting an object into the stack, We use the push() method. It inserts elements at top of the stack, Along with insertion the Top counter also increments by one.

Code:

import java.util.Stack;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{ 
		Stack<String> Stk = new Stack();
		Stk.push("Bee");
		Stk.push("Technical");
		Stk.push("is");
		Stk.push("a");
		Stk.push("Technical");
		Stk.push("Website");
		System.out.println("Stack : "+Stk);
	}
}

Output:

Output of push() operation
Output of push() operation

In the code above Stack having identifier “stk” is initialized of String data type and contains String objects as well. Stored String objects can be seen in the output.

Pop Method in Java Stack

Pop() method: For removing an object from the stack, We use the pop() function. the pop() function removes the topmost object present from the stack, Along with the removal the Top counter decrements by one.

Code:

import java.util.Stack;
class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{ 
		Stack<Integer> Stk = new Stack();
		Stk.push(1);
		Stk.push(2);
		Stk.push(7);
		Stk.push(9);
		System.out.println("Initial Stack : "+Stk);
		System.out.println("Removed top object : "+Stk.pop());
		System.out.println("Stack after removal : "+Stk);
	}
}

Output:

Output of pop() function
Output of pop() function

In the output, we can see that initially, the stack has 4 integer type objects 1,2,7, and 9 respectively. After that top most object(9) is removed by using the pop() function.

Peek Method in Java Stack Class

Peek() method for accessing the top-most object, Formally the peek object of the Stack, We use the peek() function, Which just returns the value of the object present at the top of the stack currently. It should be noted that the peek() function doesn’t remove objects from the stack. The code for the same is provided below.

Code:

import java.util.Stack;
class Main
{	
	public static void main (String[] args) throws java.lang.Exception
	{ 
		Stack<Integer> Stk = new Stack();
		Stk.push(1);
		Stk.push(2);
		Stk.push(7);
		Stk.push(9);
		System.out.println("Initial Stack : "+Stk);
		System.out.println("Peek object of Stack : "+Stk.peek());
		System.out.println("Removed top object : "+Stk.pop());
		System.out.println("Stack after removing peek object : "+Stk);
		System.out.println("Peek object currently : "+Stk.peek());
		
	}
}

Output:

Output of peek() function
Output of peek() function

In the output image above, It can be clearly seen that initially, the stack has 1,2,7, and 9 as objects. The top-most object is 9 at the current stage. When 9 is removed by performing the pop() operation, that 7 becomes a new peek element.

Empty Method in Java Stack

It is used for checking whether the Stack is empty or not, we use the empty() function of the Stack class. Which returns true if Stack is empty else returns false.

Code:

import java.util.Stack;
class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{ 
		Stack<Integer> Stk = new Stack();
		Stk.push(1);
		Stk.push(2);
		System.out.println("Stack : "+Stk);
		System.out.println("Stack is Empty? : "+Stk.empty());
		System.out.println("Removing all objects from Stack");
		Stk.pop();
		Stk.pop();
		System.out.println("Stack after removing all objects : "+Stk);
		System.out.println("Stack is Empty? : "+Stk.empty());	
	}
}

Output:

Output for empty() Function
Output for empty() Function

Initially, Stack has 1 and 2 as Integer objects. At that stage, the stack was not empty and the empty() function was returning its answer as false. After that 1 and 2 are removed by performing 2 pop() operations one by one. Both the elements were removed and then the empty() function was returning true.

Size Method in Java Stack

size() method returns the size of the Stack. Formally, the number of objects present currently in the Stack.

Code:

import java.util.Stack;
class Main
{	
	public static void main (String[] args) throws java.lang.Exception
	{ 
		Stack<Integer> Stk = new Stack();
		Stk.push(1);
		Stk.push(2);
		Stk.push(9);
		Stk.push(8);
		System.out.println("Stack  : "+Stk);
		System.out.println("Stack size is : "+Stk.size());
	}
}

Output:

Output of size() function
Output of size() function

In the output, We can see that there are 4 objects present in the stack initially, Which are 1,2,9 and 8 respectively. size() function is returning value as 4. Which shows the number of objects present in Stack currently.

Conclusion:

In the whole article, We discussed and come to know about the working of Stack and its in-built methods in Java’s Stack class.

Scroll to Top