Code explanation

in the following code don’t we have to mention or explain the code of push and pop function ? how we are using these functions dircetly ?

@dheerajmishra992,
When we create an object of stack class, we can access all the methods of that class.
Java Collection framework provides a Stack class which models and implements Stack data structure.

We create a new object named “stack” of Stack class which is inbuilt in Java collection in code line:
Stack < Integer> stack = new Stack< Integer>();

Methods in Stack class:

  1. Push :Pushes an element on the top of the stack.
  2. Pop : Removes and returns the top element of the stack. An ‘EmptyStackException’ exception is thrown if we call pop() when the invoking stack is empty.
  3. Empty : It returns true if nothing is on the top of the stack. Else, returns false.
  4. Peek : Returns the element on the top of the stack, but does not remove it.
  5. Search : It determines whether an object exists in the stack. If the element is found, it returns the position of the element from the top of the stack. Else, it returns -1.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.