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

Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Hi,

Thanks for this example (glad t

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

2. if i wanna push the data from my keyboard input, h
View Tutorial          By: andys at 2008-11-09 03:09:32

3. help me for learning to programing with java

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

4. Hi! Thanks for the examples posted reall
View Tutorial          By: creece at 2009-01-23 17:28:33

5. pls give us aprogram of postfix and infix using st
View Tutorial          By: lost_hope1111 at 2009-02-13 01:48:37

6. thanks for this example but i want the whether th
View Tutorial          By: Rupesh Chavan at 2010-03-14 07:36:31

7. lots of thanks for this given example........hope
View Tutorial          By: virgi crisostomo at 2010-08-18 17:18:08

8. hi !!
my problem is how to make a program f

View Tutorial          By: khenn reyes at 2010-12-25 05:47:08

9. please help me to do find a push and pop operation
View Tutorial          By: sweet mae at 2011-01-14 00:01:33

10. for user input use this method....
1. impor

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

11. import*;
import java.util.Stack;
View Tutorial          By: jack&jill at 2011-07-26 06:40:32

12. very good.I think if you put the other structures
View Tutorial          By: mohammad at 2011-08-08 11:01:05

13. pls give me example of stack class with postfix me
View Tutorial          By: uchiha at 2011-09-16 04:02:57

14. This helped me to make my mature cheddar cheese ap
View Tutorial          By: Randy Peterson at 2011-10-06 12:22:35

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

16. pls send to me example about stacks

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

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

18. THANKS you for having this site. it help me a lot
View Tutorial          By: fermilita at 2012-01-04 11:45:53

19. Hi Thanks for this example but i need more help fo
View Tutorial          By: hetal at 2012-02-03 08:47:39

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

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

21. thank you lot of...
this example help me...

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

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

23. Thanks a lot man !

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

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

25. please help me on "why subclass of stack is
View Tutorial          By: mahendra at 2012-12-15 03:39:22

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

27. Thanks for the example taken directly from the Jav
View Tutorial          By: Lewis Chase at 2013-09-24 23:39:21

28. aare aaighalya.....tuza chik stack madhe push kar
View Tutorial          By: chutiya at 2013-10-08 09:22:11

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

30. Please tell me how to display the contents of stac
View Tutorial          By: Senthil at 2014-05-21 21:44:05

31. import*;
import java.util.*;

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

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

33. public class zcxzcStack
//push fun

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

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

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

36. pls help on me to program in c++, write a program
View Tutorial          By: Hamid at 2014-12-18 20:41:40

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

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

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

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

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

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

43. Hey I am so grateful I foujnd your weeb site, I re
View Tutorial          By: Vashikaran Totke In Hindi at 2017-04-26 19:10:06

44. how to create a simulation of java stack without u
View Tutorial          By: cakka at 2017-07-05 00:21:12

45. I see you don't monetize your site, don't waste yo
View Tutorial          By: 86Cathern at 2017-07-20 18:05:08

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