The Java.util.Stack.isEmpty() method in Java is used to check and verify if a Stack is empty or not. It returns True if the Stack is empty else it returns False.
Syntax:
Stack.isEmpty()
Parameters: This method does not take any parameter.
Return Value: This function returns True if the Stackis empty else it returns False.
Below programs illustrate the Java.util.Stack.isEmpty() method:
Program 1:
// Java code to illustrate isEmpty() import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<String> stack = new Stack<String>(); // Use add() method to add elements into the Stack stack.add("Welcome"); stack.add("To"); stack.add("Geeks"); stack.add("4"); stack.add("Geeks"); // Displaying the Stack System.out.println("Stack: " + stack); // Verifying if the Stack is empty or not System.out.println("Is the Stack empty? " + stack.isEmpty()); // Clearing the Stack stack.clear(); // Displaying the Stack System.out.println("Stack after clear(): " + stack); // Verifying if the Stack is empty or not System.out.println("Is the Stack empty? " + stack.isEmpty()); } } |
Stack: [Welcome, To, Geeks, 4, Geeks] Is the Stack empty? false Stack after clear(): [] Is the Stack empty? true
Program 2:
// Java code to illustrate isEmpty() import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<Integer> stack = new Stack<Integer>(); // Displaying the Stack System.out.println("Stack: " + stack); // Verifying if the Stack is empty or not System.out.println("Is the Stack empty? " + stack.isEmpty()); } } |
Stack: [] Is the Stack empty? true
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java String isEmpty() method with example
- AbstractSequentialList isEmpty() method in Java with Example
- LinkedBlockingDeque isEmpty() method in Java with Example
- AbstractSet isEmpty() method in Java with Example
- ArrayList isEmpty() in Java with example
- IdentityHashMap isEmpty() Method in Java
- HashSet isEmpty() Method in Java
- TreeSet isEmpty() Method in Java
- HashMap isEmpty() Method in Java
- Hashtable isEmpty() Method in Java
- ArrayDeque isEmpty() Method in Java
- BitSet isEmpty() method in Java
- WeakHashMap isEmpty() Method in Java with Examples
- Vector isEmpty() Method in Java
- LinkedTransferQueue isEmpty() method in Java
- ConcurrentHashMap isEmpty() Method in Java
- ConcurrentSkipListSet isEmpty() method in Java
- ConcurrentLinkedQueue isEmpty() method in Java
- CopyOnWriteArrayList isEmpty() method in Java
- CopyOnWriteArraySet isEmpty() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

