JavaQuick Tip: Java Stack Implementation

Quick Tip: Java Stack Implementation

Java Developer Tutorials

Java has support for Stack implementation and its related functionalities. Below is a quick snippet of Java code showing how to implement a Stack and its functions:

*/

import java.util.*;

public class UnderstandingStack
{
	public static void main(String args[])
	{
		UnderstandingStack understandingStack = new UnderstandingStack();
		understandingStack.proceed();
	}
	
	private void proceed()
	{
		Stack stack = new Stack();
		System.out.println("stack.empty(): " + stack.empty());
		stack.push("One");
		System.out.println("stack.empty(): " + stack.empty());
		String element = (String) stack.pop();
		System.out.println("stack.pop(): " + element);
		System.out.println("stack.empty(): " + stack.empty());
	}
}

/*

Running this Java code in your IDE or code editor will produce the following output:

[root@mypc]# java UnderstandingStack
stack.empty(): true
stack.empty(): false
stack.pop(): One
stack.empty(): true

Read more Java programming and development tutorials.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories