Java Stack empty() Method Example

This example demonstrates the usage of the Stack empty() method with an example.
Stack empty() method is used to test if this stack is empty.

Java Stack empty() Method Example

Let's demonstrate the usage of an empty() method with an example:
package com.javaguides.collections.stackexamples;

import java.util.Stack;

public class StackMethodExample {

    public static void main(String[] args) {
        emptyMethod();
    }

    private static void emptyMethod() {
        // creating stack
        Stack < String > stack = new Stack < > ();

        // populating stack
        stack.push("Java");
        stack.push("JEE");
        stack.push("C");
        stack.push("C++");
        stack.push("Spring");
        stack.push("Hibernate");

        // checking stack
        System.out.println("Is stack empty: " + stack.empty());
    }
}
Output:
Is stack empty: false

Reference

Comments