pop(): This method is available in java.util.Stack class of Java.
Syntax:
K java.util.Stack.pop()
This method removes the object at the top of this stack and returns that object as the value of this function.
Parameters: NA
Returns: The object at the top of this stack.
Throws:
EmptyStackException - if this stack is empty.
Approach 1: When no exception
Java
import java.util.Stack;public class Stackpop {public static void main(String[] args) {Stack<Integer> st = new Stack<>();st.push(1);st.push(18);st.push(29);st.push(8);System.out.println(st.pop());}}
Output:
8
Approach 2: EmptyStackException
Java
import java.util.Stack;public class Stackpop {public static void main(String[] args) {Stack<Integer> st = new Stack<>();System.out.println(st.pop());}}
Output:
Exception in thread "main" java.util.EmptyStackException at java.base/java.util.Stack.peek(Stack.java:101) at java.base/java.util.Stack.pop(Stack.java:83)
No comments:
Post a Comment