Home Java Beginners Example of Java Stack Program
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Example of Java Stack Program

Advertisement
Stack is like a bucket we you can enter objects and retrieve it.

Example of Java Stack Program

     

Stack is like a bucket we you can enter objects and retrieve it. Here in the example describes the methods to prepare an example of Java stack program. The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector and implements Cloneable, Collection, List, Serializable interfaces. We mostly use push and pop operations.

In this example we are using two methods of Stack.

push(Object item): It pushes an item onto the top of this stack.

pop(): Removes the object from the top of this stack and returns that object as the value of this function. It throws EmptyStackException if the stack is empty.

Code of the program is given below

import java.util.*;

public class StackDemo{
  public static void main(String[] args) {
  Stack stack=new Stack();
  stack.push(new Integer(10));
  stack.push("a");
  System.out.println("The contents of Stack is" + stack);
  System.out.println("The size of an Stack is" + stack.size());
  System.out.println("The number poped out is" + stack.pop());
  System.out.println("The number poped out is " + stack.pop());
  //System.out.println("The number poped out is" + stack.pop());
  System.out.println("The contents of stack is" + stack);
  System.out.println("The size of an stack is" + stack.size());
  }
}

Output of this example is given below:

The contents of Stack is[10, a]
The size of an Stack is2
The number poped out isa
The number poped out is 10
The contents of stack is[]
The size of an stack is0
Hello World!

Download this example

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 7, 2007

Ask Questions?    Discuss: Example of Java Stack Program   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Odwa
April 15, 2011
Technical Programming

how to create stack class
Hemanth
June 25, 2011
Thanks

Thank you very much for this examples
Raphael
July 2, 2011
Version 1.5

Hi, could you please update this example for Java 1.5 with generics. Thanks in advance.
basha
October 25, 2011
teststring stack

in some sample of this program i notice that i can't find specific output why it only appear those unclear elements of this stack
ian
February 16, 2012
sad

why is it wrong when it comes to Stack stack=new Stack(); ?
joann
March 5, 2012
data structures

add more
anuj anand
August 21, 2012
java

this website is very helpful form me......thank's....rose india
cailyn
October 10, 2012
none

nothing
Sourabh
September 7, 2013
import java.util.Stack

For Stack class
saraswathi kalapala
December 10, 2012
print stack elements in java

please tell
DMCA.com