{"id":11591,"date":"2023-01-06T07:24:45","date_gmt":"2023-01-06T07:24:45","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=11591"},"modified":"2023-01-09T07:48:13","modified_gmt":"2023-01-09T07:48:13","slug":"stack-program-in-java","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/","title":{"rendered":"Stack Program in Java"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg\" alt=\"\" \/><\/p>\n<p>In this article, we will write a Stack program in Java. We will learn about the stack class in Java, how to create a stack, different methods of a stack in Java, and how to iterate over a stack in Java. So, let\u2019s get started.<\/p>\n<h2>What is Stack Data structure?<\/h2>\n<p>The Stack data structure is a linear data structure based on the FILO (First in Last out) or LIFO (Last in First out) principle. The word stack can be easily imagined as a pile of objects, one above the another. For instance, in a stack of books, the book kept at the top was the last element to enter the stack and if we want to empty the stack by removing the elements one by one, the book at the top of the stack will be the first element we will choose to remove from the stack as it is the most convenient one to be removed.<\/p>\n<p>So, a \u201cstack\u201d of real-world objects in general also follows the Last in First out (LIFO) principle. A stack data structure is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278323-Stack%20Program%20in%20Java1.png\" alt=\"\" \/><\/p>\n<p>So, the above image shows an empty stack. <\/p>\n<h3>Push Operation in Stack:<\/h3>\n<p>To <strong>put an element inside a stack<\/strong> is generally referred to as a <strong>push<\/strong> operation. For instance, when we push 10 in the stack, the stack will now contain 10.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278324-Stack%20Program%20in%20Java2.png\" alt=\"\" \/><\/p>\n<p>So, let us push some more elements in the stack as shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278324-Stack%20Program%20in%20Java3.png\" alt=\"\" \/><\/p>\n<p>So, we can see that the element that is pushed last into the stack is at the top of the stack. This is the general behavior of the stack data structure.<\/p>\n<h3>Pop Operation in Stack:<\/h3>\n<p>Now, let us try to remove some elements from the stack. <strong>Removing an element<\/strong> from the stack is referred to as a <strong>pop<\/strong> operation.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278346-Stack%20Program%20in%20Java4.png\" alt=\"\" \/><\/p>\n<p>So, we can see that when we pop from the stack, the topmost element of the stack gets removed. The stack after popping once is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278346-Stack%20Program%20in%20Java5.png\" alt=\"\" \/><\/p>\n<p>So, 40 is now the top of the stack. If we pop once again, then 40 will be removed from the stack as shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278351-Stack%20Program%20in%20Java6.png\" alt=\"\" \/><\/p>\n<p>So, this is what stack data structure is and the push and pop are the 2 main operations on a stack. <\/p>\n<h3>Peek Operation in Stack<\/h3>\n<p>Let us also understand the third very main operation called the <strong>top or peek of the stack.<\/strong> The top or peek simply means giving the topmost element of the stack. For instance, the top\/peek of the stack is shown below for different stacks.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278351-Stack%20Program%20in%20Java7.png\" alt=\"\" \/><\/p>\n<p>So, at any moment, the element that is the topmost element of the stack is called the peek of the stack. <\/p>\n<p>Let us now also understand the working of push, pop, and top operations in a stack.<\/p>\n<h2>Stack Program in Java &#8211; Working of Push, Pop, and Peek Operations<\/h2>\n<p>So, we have studied and understood the different operations of a Stack data structure. However, let us now understand how these operations work. Consider an empty stack shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278351-Stack%20Program%20in%20Java8.png\" alt=\"\" \/><\/p>\n<p>A stack data structure can be created using an array or a list when it comes to its implementation. <strong>An empty stack is a stack whose top = -1.<\/strong> Since the top pointer points to the topmost element of the stack and -1 is an invalid index, we can say that the stack is empty.<\/p>\n<p>What happens when we push an element into the stack? This is shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278359-Stack%20Program%20in%20Java9.png\" alt=\"\" \/><\/p>\n<p>So, <strong>when we push an element into the stack, the top pointer first increments its value<\/strong> and brings it to the position where the upcoming element (i.e. upcoming top) would be inserted.<strong>Then, at that position<\/strong> in the array or the list, <strong>we insert the element.<\/strong> This is shown both diagramatically and with the help of pseudo-code in the image above.<\/p>\n<p>Now, let us learn how the pop operation works. Consider the stack filled with some values as shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278359-Stack%20Program%20in%20Java10.png\" alt=\"\" \/><\/p>\n<p>Now, let us pop an element from this stack.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278369-Stack%20Program%20in%20Java11.png\" alt=\"\" \/><\/p>\n<p>So, the image above shows what happens when we pop an element from the stack both diagrammatically and by pseudo-code. The element at which the top pointer is pointing is <strong>first removed<\/strong> from the underlying array or list and then the <strong>value of the top pointer is decremented.<\/strong> <\/p>\n<p>What happens when we perform the operation peek()? Well, we simply return the element at the top i.e. <strong>we return the element to which the top pointer is pointing.<\/strong><\/p>\n<p>Now, just one more question left. We have seen what happens when the stack is empty. However, <strong>is a stack ever full? What happens if a stack is full?<\/strong><\/p>\n<p>So, if we implement <strong>a stack using an array<\/strong>, we know when it will be full i.e. when <strong>top = array.length<\/strong> as the array has a fixed size i.e. we are restricting the number of elements that can be pushed into the stack.<\/p>\n<p>However, if we are using a list to implement a stack, we can\u2019t say when it will be full. We can just say that if the <strong>memory limit exceeds,<\/strong> the stack will be full.<\/p>\n<p>Now that we have understood all the operations of a stack, let us create our own stack class and write a stack program in Java.<\/p>\n<p><strong>Stack Program in Java &#8211; Implementing our own Stack<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11567 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11567 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11567 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11567 .wpsm_nav-tabs > li.active > a, #tab_container_11567 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11567 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11567 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11567 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11567 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11567 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11567 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11567 .wpsm_nav-tabs > li > a:hover , #tab_container_11567 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11567 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11567 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11567 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11567 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11567 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11567 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11567 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11567 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11567 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11567 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11567 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11567\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11567\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11567_1\" aria-controls=\"tabs_desc_11567_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11567\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11567_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">class MyStack {\r\n\r\n  private int arr[];\r\n  private int top;\r\n  private int capacity;\r\n\r\n  \/\/ Creating a stack\r\n  MyStack(int size) {\r\n    arr = new int[size];\r\n    capacity = size;\r\n    top = -1;\r\n  }\r\n\r\n  public void push(int x) {\r\n    if (isFull()) {\r\n      System.out.println(\"Stack OverFlow\");\r\n\r\n      System.exit(1);\r\n    }\r\n    System.out.println(\"Inserting \" + x);\r\n    arr[++top] = x;\r\n  }\r\n\r\n  public int pop() {\r\n\r\n    if (isEmpty()) {\r\n      System.out.println(\"STACK EMPTY\");\r\n      System.exit(1);\r\n    }\r\n      \r\n    return arr[top--];\r\n  }\r\n\r\n  public int getSize() {\r\n    return top + 1;\r\n  }\r\n\r\n  public Boolean isEmpty() {\r\n    return top == -1;\r\n  }\r\n\r\n  public Boolean isFull() {\r\n    return top == capacity - 1;\r\n  }\r\n\r\n  public void printStack() {\r\n    for (int i = 0; i &lt;= top; i++) {\r\n      System.out.print(arr[i] + \", \");\r\n    }\r\n  }\r\n\r\n}\r\n\r\npublic class Main {\r\n   public static void main(String[] args) {\r\n        MyStack stack = new MyStack(5);\r\n    \r\n        stack.push(1);\r\n        stack.push(2);\r\n        stack.push(3);\r\n    \r\n        System.out.print(\"Stack: \");\r\n        stack.printStack();\r\n    \r\n        stack.pop();\r\n        System.out.println(\"&#92;nAfter popping out\");\r\n        stack.printStack();\r\n\r\n  }\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11567 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11567 a\"),jQuery(\"#tab-content_11567\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h2>Time and Space Complexities<\/h2>\n<p><strong>Push:<\/strong> The time complexity of push is O(1) as we are just adding an element to the end of the array\/list. The auxiliary space is also O(1) as we are using any extra space.<\/p>\n<p><strong>Pop:<\/strong> The time complexity of pop is also O(1) as we are just removing the last element from the array\/list. The auxiliary space is also O(1) as we are not using any extra space.<\/p>\n<p><strong>Peek():<\/strong> The time complexity of peek is also O(1) as we are just getting the last element from the array\/list. The auxiliary space is also O(1) as we are not using any extra space.<\/p>\n<p>So, we have seen how to create our own stack class in Java. However, every time we need a stack, we need not do this because Java provides us with its own Stack class that we can use. So, let\u2019s have a look at the Stack class provided by Java.<\/p>\n<h2>Stack Class in Java<\/h2>\n<p>Stack class is a part of the Java Collections Framework. It extends the Vector class and implements List, Collection, Iterable, Serializable, and Cloneable interfaces. It is a part of java.util package. Just like we create our own stack, we have predefined methods in the Stack class to push, pop, peek an element from the stack and methods to check whether the stack is empty or not, to iterate the stack, etc. The Stack program in Java using the Java Stack class is shown below.<\/p>\n<p><strong>Stack Program in Java &#8211; Using Java\u2019s Stack Class<\/strong><\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_11568 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_11568 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_11568 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_11568 .wpsm_nav-tabs > li.active > a, #tab_container_11568 .wpsm_nav-tabs > li.active > a:hover, #tab_container_11568 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_11568 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_11568 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_11568 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_11568 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_11568 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_11568 .wpsm_nav-tabs > li > a:hover , #tab_container_11568 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_11568 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_11568 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_11568 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_11568 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_11568 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_11568 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_11568 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11568 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_11568 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_11568 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_11568 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_11568\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_11568\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_11568_1\" aria-controls=\"tabs_desc_11568_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_11568\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_11568_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">import java.util.*;\r\npublic class Main {\r\n    public static void main(String[] args) {\r\n        Stack&lt;Integer&gt; stk = new Stack&lt;&gt;();\r\n        System.out.println(\"Pushing some elements in the stack\");\r\n        \/\/pushing an element into the stack -&gt; push()\r\n        stk.push(10);\r\n        stk.push(20);\r\n        stk.push(30);\r\n        stk.push(40);\r\n        stk.push(50);\r\n        \/\/display the stack\r\n        System.out.println(\"Stack: \" + stk);\r\n        \r\n        \/\/pop() -&gt; removes the element\r\n        int x = stk.pop();\r\n        System.out.println(x);\r\n        \r\n        \/\/top of the stack -&gt; peek()\r\n        System.out.println(\"The element at the top of the stack is: \" + stk.peek());\r\n        \/\/size of the stack -&gt; number of elements\r\n        System.out.println(\"Number of elements in the stack: \" + stk.size());\r\n        \/\/empty() -&gt; return true if the stack is empty, false otherwise\r\n        System.out.println(\"Is the stack empty: \" + stk.empty());\r\n        \r\n        \/\/Iterating the stack using iterator\r\n        Iterator it = stk.iterator();\r\n        while(it.hasNext()) {\r\n            Object value = it.next();\r\n            System.out.println(value);\r\n        }\r\n    }\r\n}<\/pre>\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_11568 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_11568 a\"),jQuery(\"#tab-content_11568\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Explanation:<\/strong> We have created an Integer stack in Java. We can create other stacks also like Character stack, String stack, etc. The push() function is used to push the element passed as a parameter inside the stack. The pop method removes the topmost element from the stack and also returns its value. The peek() method just returns the topmost value of the stack. The size() methods returns the number of elements in the stack. The empty() method returns a boolean value. It returns true if the stack is empty and false otherwise. Finally, we have created an iterator and have traversed the stack using it.<\/p>\n<p>So, this is how we can write a stack program in Java using the Java\u2019s stack class i.e. Java\u2019s inbuilt stack. We hope that you liked the discussion and have understood the concepts taught in this article. We hope to see you again soon at PrepBytes.<\/p>\n<p><strong>Other Java Programs<\/strong><\/p>\n<p><a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-add-two-numbers\/\" title=\"Java Program to Add Two Numbers\">Java Program to Add Two Numbers<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-check-prime-number\/\" title=\"Java Program to Check Prime Number\">Java Program to Check Prime Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-check-whether-a-number-is-a-palindrome-or-not\/\" title=\"Java Program to Check Whether a Number is a Palindrome or Not\">Java Program to Check Whether a Number is a Palindrome or Not<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-find-the-factorial-of-a-number\/\" title=\"Java Program to Find the Factorial of a Number\">Java Program to Find the Factorial of a Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-reverse-a-number\/\" title=\"Java Program to Reverse a Number\">Java Program to Reverse a Number<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-program-to-search-an-element-in-a-linked-list\/\" title=\"Java Program to search an element in a Linked List\">Java Program to search an element in a Linked List<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/program-to-convert-arraylist-to-linkedlist-in-java\/\" title=\"Program to convert ArrayList to LinkedList in Java\">Program to convert ArrayList to LinkedList in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/java-program-to-reverse-a-linked-list\/\" title=\"Java Program to Reverse a linked list\">Java Program to Reverse a linked list<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/java-program-to-search-an-element-in-a-linked-list\/\" title=\"Java Program to search an element in a Linked List\">Java Program to search an element in a Linked List<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/anagram-program-in-java\/\" title=\"Anagram Program in Java\">Anagram Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/inheritance-program-in-java\/\" title=\"Inheritance Program in Java\">Inheritance Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/even-odd-program-in-java\/\" title=\"Even Odd Program in Java\">Even Odd Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/hello-world-program-in-java\/\" title=\"Hello World Program in Java\">Hello World Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/if-else-program-in-java\/\" title=\"If else Program in Java\">If else Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/binary-search-program-in-java\/\" title=\"Binary Search Program in Java\">Binary Search Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/linear-search-program-in-java\/\" title=\"Linear Search Program in Java\">Linear Search Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/menu-driven-program-in-java\/\" title=\"Menu Driven Program in Java\">Menu Driven Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/package-program-in-java\/\" title=\"Package Program in Java\">Package Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/leap-year-program-in-java\/\" title=\"Leap Year Program in Java\">Leap Year Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/array-programs-in-java\/\" title=\"Array Programs in Java\">Array Programs in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/linked-list-program-in-java\/\" title=\"Linked List Program in Java\">Linked List Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/string-programs-in-java\/\" title=\"String Programs in Java\">String Programs in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/star-program-in-java\/\" title=\"Star Program in Java\">Star Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/number-pattern-program-in-java\/\" title=\"Number Pattern Program in Java\">Number Pattern Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/for-loop-program-in-java\/\" title=\"For Loop Program In Java\">For Loop Program In Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/pattern-program-in-java\/\" title=\"Pattern Program in Java\">Pattern Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/string-palindrome-program-in-java\/\" title=\"String Palindrome Program in Java\">String Palindrome Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/thread-program-in-java\/\" title=\"Thread Program in JAVA\">Thread Program in JAVA<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/java-scanner-program\/\" title=\"Java Scanner Program\">Java Scanner Program<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/while-loop-program-in-java\/\" title=\"While Loop Program in Java\">While Loop Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/bubble-sort-program-in-java\/\" title=\"Bubble Sort Program in Java\">Bubble Sort Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/fibonacci-series-program-in-java\/\" title=\"Fibonacci Series Program in Java\">Fibonacci Series Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/calculator-program-in-java\/\" title=\"Calculator Program in Java\">Calculator Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/fizzbuzz-program-in-java\/\" title=\"Fizzbuzz Program in Java\">Fizzbuzz Program in Java<\/a><br \/>\n<a href=\"https:\/\/prepbytes.com\/blog\/java\/matrix-program-in-java\/\" title=\"Matrix Program in Java\">Matrix Program in Java<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will write a Stack program in Java. We will learn about the stack class in Java, how to create a stack, different methods of a stack in Java, and how to iterate over a stack in Java. So, let\u2019s get started. What is Stack Data structure? The Stack data structure is [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[143],"tags":[],"class_list":["post-11591","post","type-post","status-publish","format-standard","hentry","category-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Stack Program in Java<\/title>\n<meta name=\"description\" content=\"We will learn about the stack class in Java, how to create a stack, different methods of a stack in Java, and how to iterate over a stack in Java.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stack Program in Java\" \/>\n<meta property=\"og:description\" content=\"We will learn about the stack class in Java, how to create a stack, different methods of a stack in Java, and how to iterate over a stack in Java.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-06T07:24:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-09T07:48:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Stack Program in Java\",\"datePublished\":\"2023-01-06T07:24:45+00:00\",\"dateModified\":\"2023-01-09T07:48:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/\"},\"wordCount\":1486,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/\",\"name\":\"Stack Program in Java\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg\",\"datePublished\":\"2023-01-06T07:24:45+00:00\",\"dateModified\":\"2023-01-09T07:48:13+00:00\",\"description\":\"We will learn about the stack class in Java, how to create a stack, different methods of a stack in Java, and how to iterate over a stack in Java.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/java\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Stack Program in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stack Program in Java","description":"We will learn about the stack class in Java, how to create a stack, different methods of a stack in Java, and how to iterate over a stack in Java.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Stack Program in Java","og_description":"We will learn about the stack class in Java, how to create a stack, different methods of a stack in Java, and how to iterate over a stack in Java.","og_url":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-01-06T07:24:45+00:00","article_modified_time":"2023-01-09T07:48:13+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Stack Program in Java","datePublished":"2023-01-06T07:24:45+00:00","dateModified":"2023-01-09T07:48:13+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/"},"wordCount":1486,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/","url":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/","name":"Stack Program in Java","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg","datePublished":"2023-01-06T07:24:45+00:00","dateModified":"2023-01-09T07:48:13+00:00","description":"We will learn about the stack class in Java, how to create a stack, different methods of a stack in Java, and how to iterate over a stack in Java.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/stack-program-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1672987278247-Stack%20Program%20in%20Java.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/stack-program-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/prepbytes.com\/blog\/category\/java\/"},{"@type":"ListItem","position":3,"name":"Stack Program in Java"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=11591"}],"version-history":[{"count":1,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11591\/revisions"}],"predecessor-version":[{"id":11592,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/11591\/revisions\/11592"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=11591"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=11591"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=11591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}