- 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 pop() Method
Description
The pop() method is used to remove the object at the top of this stack and returns that object as the value of this function.
Declaration
Following is the declaration for java.util.Stack.pop() method.
public Object pop()
Parameters
NA
Return Value
The method call returns the object at the top of this stack.
Exception
EmptyStackException − This is thrown if this stack is empty.
Get Integer from the Top of Stack Example
The following example shows the usage of Java Stack pop() method to get the integer from the top of the stack after removing it from the stack. In this example, we've created a Stack object of Integers. Then we've added few integers to the stack and printed the top element after removing it using pop() method. Then we've printed the stack to see the remaining elements.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<Integer> st = new Stack<>(); // populating stack st.push(10); st.push(20); st.push(30); // removing top integer System.out.println("Removed integer is: "+st.pop()); // elements after remove System.out.println("Elements after remove: "+st); } }
Let us compile and run the above program, this will produce the following result.
Removed integer is: 30 Elements after remove: [10, 20]
Get String from the Top of Stack Example
The following example shows the usage of Java Stack pop() method to get the String from the top of the stack after removing it from the stack. In this example, we've created a Stack object of Strings. Then we've added few strings to the stack and printed the top string after removing it using pop() method. Then we've printed the stack to see the remaining elements.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<String> st = new Stack<>(); // populating stack st.push("Java"); st.push("Source"); st.push("code"); // removing top string System.out.println("Removed string is: "+st.pop()); // elements after remove System.out.println("Elements after remove: "+st); } }
Let us compile and run the above program, this will produce the following result.
Removed string is: code Elements after remove: [Java, Source]
Get Object from the Top of Stack Example
The following example shows the usage of Java Stack pop() method to get the Student object from the top of the stack after removing it from the stack. In this example, we've created a Stack of Student objects. Then we've added few students to the stack and printed the top element after removing it using pop() method. Then we've printed the stack to see the remaining elements.
package com.tutorialspoint; import java.util.Stack; public class StackDemo { public static void main(String args[]) { // creating stack Stack<Student> st = new Stack<>(); // populating stack st.push(new Student(1, "Julie")); st.push(new Student(2, "Robert")); st.push(new Student(3, "Adam")); // removing top student System.out.println("Removed student is: "+st.pop()); // elements after remove System.out.println("Elements after remove: "+st); } } 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.
Removed student is: [ 3, Adam ] Elements after remove: [[ 1, Julie ], [ 2, Robert ]]
To Continue Learning Please Login
Login with Google