Stack using arrays / Push(), Pop(), and Display() methods in JAVA Programming Language


Write a Java program to implement the Stack using arrays. Write Push(), Pop(), and Display() methods to demonstrate its working.


In Java everything is encapsulated under classes. The class is the core of Java language. The class can be defined as a template/ blueprint that describes the behaviors/states of a particular entity. A class defines the new data type. This type can be used to create an object of that type. An object is an instance of the class. You may also call it as physical existence of a logical template class. A class is declared using class keyword. A class contains both data and code that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods.

A simple class example:

class Student{
String USN,name , branch;
int phoneno;

Object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior. An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables. Creating variables of your class type is similar to creating variables of primitive data types, such as integer or float. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object. If you want to create an object of the class and have the reference variable associated with this object, you must also allocate memory for the object by using the new operator. This process is called instantiating an object or creating an object instance.

In the following statement, obj is instance/object of Student class.

Student obj=new Student();

An array of objects is created just like an array of primitive type data items in the following way.

Student[] obj_Array = new Student[7];

However, in this particular case, we may use a for a loop since all Student objects are created with the same default constructor.

for ( int i=0; i<obj_Array.length; i++) {
obj_Array[i]=new Student();


The Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as the Constructor.

Types of java constructors

There are two types of constructors:

  • Default constructor (no-arg constructor)
  • Parameterized constructor

A constructor that have no parameter is known as default constructor.

//block of code

Object creation:

Student obj=new Student();

Constructor with arguments is known as parameterized constructor.

Student(int i,String n){
id = i;
name = n;

Object creation:

Student4 s1 = new Student4(350,"Rabins");


import java.util.Scanner;
public class stack
{ int element,maxsize,top;
int[] st;
public stack()
{ Scanner integer=new Scanner(;
System.out.println("Enter stack size");
st=new int[maxsize];
public void push(int element)
{ /*if(top>=maxsize)
{ System.out.println("Overflow!!");
{ st[++top]=element;
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println(e);
public int pop()
{ if(top==-1)
{ System.out.println("UnderFlow");
public void display(int[] st, int max_size)
{ int i;
System.out.println("Stack Elements:");
import java.util.Scanner;
public class myStack
{ static int option;
public static void main(String[] args)
{ stack obj=new stack();
{ System.out.println("\nEnter yours choice\n1.PUSH\n2.POP\n3.Display\n4..EXIT");
Scanner integer=new Scanner(;
{ case 1: System.out.println("Enter Element");
case 2:System.out.printf("Poped element is %d",obj.pop());
case 3:obj.display(,;
case 4:System.exit(0);
default:System.out.println("Wrong option");


JAVA Stack operations - Push, Pop, display

Output of JAVA Stack operations – Push, Pop, display

Incoming search terms:
  • arraystack push pop java
  • Write a program to demonstrate list and stack operations in java
  • stack pop method java
  • stack array using pop and push method
  • Implement working of stack push and pop opertation
  • array pop java
  • write a stack array in java
  • stack display java code
  • java stack pop push implementation best
  • how to write program in java in stacks
  • how to display a stack
  • java prg to demonstrate stack using interface concept with void push(int)
  • push and pop program in java
  • push and pop operations using interface in java
  • java stack pop push array
  • stack program using java
  • Write a Java program to implement the Stack using arrays Write Push() Pop() and Display() methods to demonstrate its working
  • class using stack of array in java push pop display
  • stack program pull and pop in java
  • compare string using push pop method stack java
  • stack pop and push using array
  • write a program to push and didplay
  • creating a stack using arrays in java
  • dispaly the valus using stack operation in interface
  • pushing popping from array java
  • pushing and popping arrays java
  • push operation in stack array in java
  • push operation in java
  • push and pop stack array
  • how to push and pop in a stack using array
  • ahow to use pop in array in stack in java
  • push and pop multiple stacks in java
  • push and pop array java
  • push (stack top maxstk item) java program
  • psuah and pop coding using java
  • program on stacks using arrays in java
  • perform push and pop operations on stack implement stacks and avoid using inbuilt library in java
  • how do you push a string into a program stack java using nodes
  • java recursion push pop
  • java push pop
  • java program to push pop display in stack using recursion
  • java prog to perform push pop display the stack
  • java display a stack
  • int push and pop in java
  • implement working of stack using pop method
  • how to combine a stack top and pop operation in a stack in java

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing the Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. Rabins is also the first initiator of Digital Nepal.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *