Stack isEmpty() method in Java with Example
Last Updated :
24 Dec, 2018
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:
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack<String> stack = new Stack<String>();
stack.add( "Welcome" );
stack.add( "To" );
stack.add( "Geeks" );
stack.add( "4" );
stack.add( "Geeks" );
System.out.println( "Stack: " + stack);
System.out.println( "Is the Stack empty? "
+ stack.isEmpty());
stack.clear();
System.out.println( "Stack after clear(): "
+ stack);
System.out.println( "Is the Stack empty? "
+ stack.isEmpty());
}
}
|
Output:
Stack: [Welcome, To, Geeks, 4, Geeks]
Is the Stack empty? false
Stack after clear(): []
Is the Stack empty? true
Program 2:
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack<Integer> stack = new Stack<Integer>();
System.out.println( "Stack: " + stack);
System.out.println( "Is the Stack empty? "
+ stack.isEmpty());
}
}
|
Output:
Stack: []
Is the Stack empty? true
Share your thoughts in the comments
Please Login to comment...