Stack implementation using array program

Java Program to implement stack using array

class Main {
    int MAX = 500;
    int top=-1;
    int a[] = new int[MAX];  
    public void push(int x)
    {
        if (top >= (MAX - 1)) {
            System.out.println("Stack Overflow");
        }
        else {
            a[++top] = x;
            System.out.println("Item pushed into stack = "+x);
        }
    }
 
    public int pop()
    {
        if (top < 0) {
            System.out.println("Stack Underflow");
            return 0;
        }
        else {
            int x = a[top--];
            return x;
        }
    }
 
    public int peek()
    {
        if (top < 0) {
            System.out.println("Stack Underflow");
            return 0;
        }
        else {
            int x = a[top];
            return x;
        }
    }

public void display(){
	if (top < 0) {
            System.out.println("No elements in Stack");
        }
        else {
            for(int i=0;i<=top;i++){
		 System.out.println(a[i]);
		}
        }

}
    public static void main(String args[])
    {
        Main m = new Main();
        m.push(10);
        m.push(20);
        m.push(30);
        System.out.println("Item popped from stack = "+m.pop());
        System.out.println(m.peek() + " Returned by Peek operation");
        System.out.println("Elements in Stack are");
        m.display();
    }
}

Output

stack implementation using array in java