Home Java Beginners Example of Java Stack Program



Example of Java Stack Program
Posted on: June 7, 2007 at 12:00 AM
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

Related Tags for Example of Java Stack Program:
javacclassliststlinterfaceobjectvectorioobjectsmethodsinterfacesstackmethodclonecollectionserialintriaoperaifexamplepushprogramtoramexamextendastshexteitlastextendsdesserializableliusepepopimfirstceinoperationsasstamntoutparososjoperationaceclesfacesemendmeobjproackdescribetorxaxampsctorcolatratiokisirllcollectmplpreeaandarcollectreprxtvazscrssrithcloneableavbesstabablatictsctofaceendsendslifjepleplprmostndodsonogrolo


More Tutorials from this section

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.