- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Stack empty() Method
Description
The Java Stack 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
Empty check of Stack of Integers Example
The following example shows the usage of Java Stack empty() method to check if a stack is empty or not. In this example, we've created a Stack object of Integers. As first step, we've printed the status of stack as empty using empty() method. Then we've added few integers to the stack and printed the status of stack as non-empty using empty() method.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<Integer> st = new Stack<>(); // checking stack System.out.println("Is stack empty: "+st.empty()); // populating stack st.push(10); st.push(20); st.push(30); // 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: true Is stack empty: false
Empty check of Stack of Strings Example
The following example shows the usage of Java Stack empty() method to check if a stack is empty or not. In this example, we've created a Stack object of Strings. As first step, we've printed the status of stack as empty using empty() method. Then we've added few strings to the stack and printed the status of stack as non-empty using empty() method.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<String> st = new Stack<>(); // checking stack System.out.println("Is stack empty: "+st.empty()); // populating stack st.push("tutorials"); st.push("points"); st.push(".com"); // 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: true Is stack empty: false
Empty check of Stack of Objects Example
The following example shows the usage of Java Stack empty() method to check if a stack is empty or not. In this example, we've created a Stack object of Student objects. As first step, we've printed the status of stack as empty using empty() method. Then we've added few students to the stack and printed the status of stack as non-empty using empty() method.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<Student> st = new Stack<>(); // checking stack System.out.println("Is stack empty: "+st.empty()); // populating stack st.push(new Student(1, "Julie")); st.push(new Student(2, "Robert")); st.push(new Student(3, "Adam")); // checking stack System.out.println("Is stack empty: "+st.empty()); } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } }
Let us compile and run the above program, this will produce the following result.
Is stack empty: true Is stack empty: false
To Continue Learning Please Login
Login with Google