java.util.Stack.empty() Method

Advertisements


Description

The empty() method is used to test if this stack is or not.

Declaration

Following is the declaration for java.util.Stack.empty() method.

public boolean empty()

Parameters

  • NA

Return Value

The method call returns 'true' if and only if this stack contains no items; false otherwise.

Exception

  • NA

Example

The following example shows the usage of java.util.Stack.empty()

package com.tutorialspoint;

import java.util.*;

public class StackDemo {
   public static void main(String args[]) {
      // creating stack
      Stack st = new Stack();
      
      // populating stack
      st.push("Java");
      st.push("Source");
      st.push("code");
      
      // checking stack
      System.out.println("Is stack empty: "+st.empty());
   }    
}

Let us compile and run the above program, this will produce the following result.

Is stack empty: false



Advertisements
Advertisements