Java Stack Program


Java Stack Program

Summary: By the end of this tutorial "Java Stack Program", you will understand stack in Java. Very comfortable with the explaination and code examples.

Stack, in any language, stores elements in LIFO (Last-in First-out) order. As the name indicates, in LIFO, the last added element is retrieved first. It can be imagined like a stack of plates put on the top of the other. The plate added at the end is drawn first. In stack, elements cannot be added in the middle or removed. There can be only one point of entry and one point of exit.

Following is the class signature

public class Stack extends Vector

Java Stack is derived from Vector and again Vector is derived from List. List belongs to collections framework (JDK 1.2) and Stack and Vector belong to legacy classes (JDK 1.0 DS are known as legacy classes; they are Stack, Vector, Hashtable and Properties).

The Java designers extended List to Stack and Vector to give the features and advantages of collections (JDK 1.2 DS) to legacy classes (JDK 1.0 DS). It is a good designing principle. Now stack and vector can use all the methods of Collection and List interfaces.

The java.util.Stack comes with five operations represented by five methods.

  1. Object push(Object obj): An element obj is added the to the stack with push() method. Elements are added implicitly on the top of another. The method returns the element added.
  2. Object pop(): With this method, the elements can be retrieved from the Stack. The last one added (which lies on the top) is returned first. With pop(), the element comes out of the stack permanently. That is, it cannot be retrieved again.
  3. int search(Object obj): With this method, an element, obj, index number can be obtained. The counting starts from the top. The topmost element index number is 1. If the stack is empty, the method returns EmptyStackException.
  4. Object peek(): This method returns the element existing on the top. But element is not popped (removed) out. This method is best useful to know whether an element exists or not.
  5. boolean empty(): Returns true if no elements exist in the stack.
pop vs peek

For a beginner, these two methods are very confusing. Let us discuss again. The pop() method returns the element existing on the top of the stack. The element returned is deleted from the stack permanently. That is, the programmer cannot obtain it again.

The peek() method also returns the element but the element is not deleted from the stack. That is, calling peek() method five times, returns the same element 5 times. peek() method can be used to know which element is lying on the top of the stack or which element will returned when pop() is called. It can be used in regular programming like this.

       if(st.peek().equals("SNRao"))
       {
          System.out.println(st.pop());
       }

Limitations of Stack Usage

There are two limitations for stack usage.

  1. The programmer stores data in a DS with an intention to get the elements whenever he would like in the code. But with stack, the popped element is completely removed and cannot be obtained again. For this reason stack is not used much in general production software like health and banking etc, but used by system and tool developers.
  2. Another drawback is, the elements cannot be added or retrieved from the middle.

The Java Stack class comes with only one constructor.

  • Stack(): This default constructor creates an empty Stack object.

14 thoughts on “Java Stack Program”

    1. Enums

      JDK 1.5 supports enumerated types. The “enum” is a special type of class. The enum type puts restriction on the instance variables. It is a keyword of Java from JDK 1.5. The variables of enum are by default integer values and also static and final. Because variables are final (constants), they are written in uppercase, by convention. Enums are type-safe.

      enum StudentGrades
      {
      A, B, C, D
      }
      public class EnumDemo
      {
      public static void main(String args[])
      {
      StudentGrades sg;
      sg = StudentGrades.C;
      // printing enum value
      System.out.println(“Value of sg: ” + sg + “\n”);
      // assign one more value
      sg = StudentGrades.A;
      // comparing two enum values
      if(sg == sg.A)
      {
      System.out.println(“Student is intelligent\n”);
      }
      // using switch statement with enum
      switch(sg)
      {
      case A:
      System.out.println(“Intelligent”); break;
      case B:
      System.out.println(“Clever”); break;
      case C:
      System.out.println(“Medium”); break;
      case D:
      System.out.println(“Poor”);
      }
      }
      }

      Because enum values are integer values, enum can be used in combination with switch statement as in the above program.

      Programming Tip: Do not confuse enum with java.util.Enumeration interface (exists from JDK 1.0). Both are very different.

Leave a Comment

Your email address will not be published.