- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check whether a Stack is empty or not in Java
The method java.util.Stack.empty() is used to check if a stack is empty or not. This method requires no parameters. It returns true if the stack is empty and false if the stack is not empty.
A program that demonstrates this is given as follows −
Example
import java.util.Stack; public class Demo { public static void main (String args[]) { Stack stack = new Stack(); stack.push("Amy"); stack.push("John"); stack.push("Mary"); System.out.println("The stack elements are: " + stack); System.out.println("The stack is empty? " + stack.empty()); System.out.println("
The element that was popped is: " + stack.pop()); System.out.println("The element that was popped is: " + stack.pop()); System.out.println("The element that was popped is: " + stack.pop()); System.out.println("
The stack elements are: " + stack); System.out.println("The stack is empty? " + stack.empty()); } }
Output
The stack elements are: [Amy, John, Mary] The stack is empty? false The element that was popped is: Mary The element that was popped is: John The element that was popped is: Amy The stack elements are: [] The stack is empty? true
Now let us understand the above program.
The Stack is created. Then Stack.push() method is used to add the elements to the stack. The stack is displayed and then the Stack.empty() method is used to check if a stack is empty or not. A code snippet which demonstrates this is as follows −
Stack stack = new Stack(); stack.push("Amy"); stack.push("John"); stack.push("Mary"); System.out.println("The stack elements are: " + stack); System.out.println("The stack is empty? " + stack.empty());
The Stack.pop() method is used to pop the three stack elements. The stack is displayed and then the Stack.empty() method is used to check if a stack is empty or not. A code snippet which demonstrates this is as follows −
System.out.println("
The element that was popped is: " + stack.pop()); System.out.println("The element that was popped is: " + stack.pop()); System.out.println("The element that was popped is: " + stack.pop()); System.out.println("
The stack elements are: " + stack); System.out.println("The stack is empty? " + stack.empty());
- Related Articles
- Check whether a HashSet is empty or not in Java
- Check whether a NavigableMap empty or not in Java
- Check whether IdentityHashMap empty or not in Java?
- C# program to check whether a list is empty or not
- Python program to check whether a list is empty or not?
- Check whether a character is Lowercase or not in Java
- Check whether a character is Uppercase or not in Java
- Java Program to check if a string is empty or not
- Check whether a field is empty or null in MySQL?
- Check Whether a Number is a Coprime Number or Not in Java
- Check whether the file is hidden or not in Java
- Check whether the entered value is a digit or not in Java
- Check whether the entered value is a letter or not in Java
- Check Whether a Number is an Autobiographical Number or Not in JAVA
- Java Program to Check Whether a Character is Alphabet or Not

Advertisements