Stack in Java

Hi Programmers , Welcome to new Article of Ozanecare. This article i am going to explain Stack programming in java . In java we should use Stack class , It is based on the principle of LIFO (Last in First Out). The two important methods used push and pop. the push method pushes an element on the top of the stack. pop method removes and returns top of the element in the Stack. see below image.

Example 1 of Stack Programming.
peek method returns top of the element in the Stack. count method count total numbers of element in stack. Contains method check element present in the stack or not. if present then return true else false. see below image.

Example 2 of Stack Programming.
Stack class with String , it means stack can store only string values. if you used search method and element not found by search then it return -1 else return element position value. see below image.

Happy Coding…Thanks.

Post Author: adama