Stack example in Java - push(), pop(), empty(), search()

By: Henry  

Stack is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own.

To put an object on the top of the stack, call push(). To remove and return the top element, call pop(). An EmptyStackException is thrown if you call pop( ) when the invoking stack is empty. You can use peek( ) to return, but not remove, the top object.

The empty() method returns true if nothing is on the stack. The search() method determines whether an object exists on the stack, and returns the number of pops that are required to bring it to the top of the stack. Here is an example that creates a stack, pushes several Integer objects onto it, and then pops them off again:

// Demonstrate the Stack class.
import java.util.*;
class StackDemo {
static void showpush(Stack st, int a) {
st.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
static void showpop(Stack st) {
System.out.print("pop -> ");
Integer a = (Integer) st.pop();
System.out.println("stack: " + st);
public static void main(String args[]) {
Stack st = new Stack();
System.out.println("stack: " + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
try {
} catch (EmptyStackException e) {
System.out.println("empty stack");

The following is the output produced by the program; notice how the exception handler for EmptyStackException is caught so that you can gracefully handle a stack underflow:

stack: [ ]
stack: [42]
stack: [42, 66]
stack: [42, 66, 99]
pop -> 99
stack: [42, 66]
pop -> 66
stack: [42]
pop -> 42
stack: [ ]
pop -> empty stack

Archived Comments

1. Have fun with your Mii on the Nintendo social app.
View Tutorial          By: Adela at 2017-08-06 01:54:31

2. I see you don't monetize your site, don't waste your traffic, you can earn extra bucks every month b
View Tutorial          By: 86Cathern at 2017-07-20 18:05:08

3. how to create a simulation of java stack without using internal library ?
View Tutorial          By: cakka at 2017-07-05 00:21:12

4. Hey I am so grateful I foujnd your weeb site, I really found
yoou by mistake, while I was re

View Tutorial          By: Vashikaran Totke In Hindi at 2017-04-26 19:10:06

5. Guestchoob
View Tutorial          By: Guestchoob at 2017-03-26 18:52:26

6. Guestchoob
View Tutorial          By: Guestchoob at 2017-02-23 12:23:58

7. how to implement stack with using vector class
View Tutorial          By: anamika at 2015-08-25 18:58:29

8. can i using stack in microsoft access 2007
View Tutorial          By: pxin at 2015-03-24 05:45:56

9. that is fifo rigth? how about lifo?
View Tutorial          By: galz at 2015-02-18 03:03:31

10. why push is error in st.push(new Integer(a)); ? and pop in Integer a = (Integer) st.pop();??
View Tutorial          By: ann at 2015-02-02 09:23:41

11. pls help on me to program in c++, write a program in c++ (1)enter 10 integer (2) pushing on stack/ p
View Tutorial          By: Hamid at 2014-12-18 20:41:40

12. thanks bro n SEe myblog <a href="
View Tutorial          By: Arif at 2014-12-03 05:12:02

13. How can I use stack in entering a letter and will reverse immediately?
View Tutorial          By: mards at 2014-10-17 12:18:34

14. public class zcxzcStack
//push function/method
// please change the code than

View Tutorial          By: eissen at 2014-09-22 13:32:20

15. walay boot ang program
View Tutorial          By: jampong at 2014-08-27 07:52:32

16. import*;
import java.util.*;
class Stackdemo
public static v

View Tutorial          By: srikanth at 2014-05-30 06:44:58

17. Please tell me how to display the contents of stack for this program
View Tutorial          By: Senthil at 2014-05-21 21:44:05

18. There is another implementation of stack I saw at:
View Tutorial          By: John Smith at 2014-02-11 09:50:05

19. aare aaighalya.....tuza chik stack madhe push kar n tondat pop kar....thoda time zalyawr chaddi cha
View Tutorial          By: chutiya at 2013-10-08 09:22:11

20. Thanks for the example taken directly from the Java Software Structures book by Dr. Chase and Dr. Le
View Tutorial          By: Lewis Chase at 2013-09-24 23:39:21

21. sex
View Tutorial          By: sex at 2013-09-07 09:22:48

22. please help me on "why subclass of stack is freeze in collection of java frame work? "
View Tutorial          By: mahendra at 2012-12-15 03:39:22

23. please give some examples of postfix and infix.
View Tutorial          By: john mark at 2012-10-07 13:55:04

24. Thanks a lot man !

View Tutorial          By: Karan Vohra at 2012-08-30 02:15:05

25. Thnx A Lot...very healpful...........
View Tutorial          By: vinay at 2012-07-19 09:21:20

26. thank you lot of...
this example help me... it is very good.
but do you can give me a

View Tutorial          By: Lam Hoang Thy at 2012-05-04 01:11:20

27. Thank you lot [email protected]
This example help me.

View Tutorial          By: Tushar at 2012-02-12 02:48:10

28. Hi Thanks for this example but i need more help for the following program:
int n = 4;

View Tutorial          By: hetal at 2012-02-03 08:47:39

29. THANKS you for having this site. it help me a lot on my report..
but I am more please if its

View Tutorial          By: fermilita at 2012-01-04 11:45:53

30. Thanks
View Tutorial          By: PARIKSHIT SHARMA at 2011-12-23 22:33:39

31. pls send to me example about stacks
because i cant understand it

View Tutorial          By: suliman at 2011-12-10 08:02:01

32. can u give me a codes for prefix to infix convertion?
View Tutorial          By: bangskie at 2011-10-10 08:11:46

33. This helped me to make my mature cheddar cheese application. I am from Florida. Power.
View Tutorial          By: Randy Peterson at 2011-10-06 12:22:35

34. pls give me example of stack class with postfix method and pop and push...tnx
View Tutorial          By: uchiha at 2011-09-16 04:02:57

35. very good.I think if you put the other structures in your site is very good.
View Tutorial          By: mohammad at 2011-08-08 11:01:05

36. import*;
import java.util.Stack;
public class Stacks {
public stat

View Tutorial          By: jack&jill at 2011-07-26 06:40:32

37. for user input use this method....
1. import*;
2.for input:

View Tutorial          By: susmit at 2011-03-02 09:09:46

38. please help me to do find a push and pop operation with stack operation using the case statement
View Tutorial          By: sweet mae at 2011-01-14 00:01:33

39. hi !!
my problem is how to make a program for stacks by using switch method?
hi !!
View Tutorial          By: khenn reyes at 2010-12-25 05:47:08

40. lots of thanks for this given example........hope u could create more example in java programming...
View Tutorial          By: virgi crisostomo at 2010-08-18 17:18:08

41. thanks for this example but i want the whether there is a complexity of the stack is in array or t
View Tutorial          By: Rupesh Chavan at 2010-03-14 07:36:31

42. pls give us aprogram of postfix and infix using stack java....pls. send in this email tnx so much...
View Tutorial          By: lost_hope1111 at 2009-02-13 01:48:37

43. Hi! Thanks for the examples posted really helps me a lot! Hope we can learn more from you
View Tutorial          By: creece at 2009-01-23 17:28:33

44. help me for learning to programing with java
iam from iraq

View Tutorial          By: hemn at 2008-11-25 03:12:19

45. if i wanna push the data from my keyboard input, how the syntax is?
View Tutorial          By: andys at 2008-11-09 03:09:32

46. Hi,

Thanks for this example (glad to have found this site! Bookmarked it too) however

View Tutorial          By: Thor at 2007-10-06 15:12:28

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial