Stack empty() Method in Java

The java.util.Stack.empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false.

Syntax:

STACK.empty()

Parameters: The method does not take any parameters.

Return Value: The method returns boolean true if the stack is empty else it returns false.

Below programs illustrate the working of java.util.Stack.empty() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate empty() method
import java.util.*;
  
public class Stack_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty Stack
        Stack<String> STACK = new Stack<String>();
  
        // Stacking strings
        STACK.push("Geeks");
        STACK.push("4");
        STACK.push("Geeks");
        STACK.push("Welcomes");
        STACK.push("You");
  
        // Displaying the Stack
        System.out.println("The stack is: " + STACK);
  
        // Checking for the emptiness of stack
        System.out.println("Is the stack empty? "
                                      STACK.empty());
  
        // Popping out all the elements
        STACK.pop();
        STACK.pop();
        STACK.pop();
        STACK.pop();
        STACK.pop();
  
        // Checking for the emptiness of stack
        System.out.println("Is the stack empty? "
                                     STACK.empty());
    }
}

chevron_right


Output:

The stack is: [Geeks, 4, Geeks, Welcomes, You]
Is the stack empty? false
Is the stack empty? true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to demonstrate empty() method
import java.util.*;
  
public class Stack_Demo {
    public static void main(String[] args)
    {
  
        // Creating an empty Stack
        Stack<Integer> STACK = new Stack<Integer>();
  
        // Stacking int values
        STACK.push(8);
        STACK.push(5);
        STACK.push(9);
        STACK.push(2);
        STACK.push(4);
  
        // Displaying the Stack
        System.out.println("The stack is: " + STACK);
  
        // Checking for the emptiness of stack
        System.out.println("Is the stack empty? "
                                      STACK.empty());
    }
}

chevron_right


Output:

The stack is: [8, 5, 9, 2, 4]
Is the stack empty? false


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

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.