{"id":13630,"date":"2020-10-20T12:27:21","date_gmt":"2020-10-20T19:27:21","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=13630"},"modified":"2023-12-01T04:03:14","modified_gmt":"2023-12-01T12:03:14","slug":"java-stack","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/java-stack\/","title":{"rendered":"Java Stack Class"},"content":{"rendered":"\n<p><em>A Java stack is a last-in, first-out data structure. The first item removed from a stack is the item last added to the stack. A stack data structure adds new items to the end of the stack. Java stacks extend the Vector class.<\/em><\/p>\n\n\n\n<p>In programming, a stack is a last-in, first-out data structure used to store data. Stacks are useful in a variety of cases. For example, if you want to test symmetry in a list or reverse the order of a list, you can use a stack.<\/p>\n\n\n\n<p>The Java collection framework includes a class called <em>stack<\/em> that is used to build stacks in Java. This tutorial will discuss the basics of stacks in Java, how to create a stack, and the main methods the stack class offers. We\u2019ll refer to examples throughout to reinforce how the stack class works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Stacks<\/h2>\n\n\n\n<p>A Java stack stores elements in a last-in, first-out (LIFO) structure. This means that the element added to the top of the stack will be the first one removed from the stack.<\/p>\n\n\n\n<p>One example of a stack in <a href=\"https:\/\/careerkarma.com\/blog\/learn-to-code-for-free\/\">programming<\/a> would be in a web browser\u2019s page control features. When you visit a web page, your web browser adds it to a record of visited pages. This record is a stack.&nbsp;<\/p>\n\n\n\n<p>Each time you visit a new web page during a browsing session, your browser adds a new entry to the browsing stack. If you want to return to the last page you visited (<em>last-in<\/em>), your browser will remove the most recent entry from the stack first (<em>first-out<\/em>). Here\u2019s a table that shows an example stack:<\/p>\n\n\n\n<table class=\"wp-block-table course-info-table\"><tbody><tr><td><strong>Site Name<\/strong><\/td><\/tr><tr><td>google.com<\/td><\/tr><tr><td>nytimes.com<\/td><\/tr><tr><td>careerkarma.com<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>This is our browsing history stack. We are currently on the google.com main page. Before the google.com page, we were on the <em>New York Times<\/em> main page. Before that, we were on the Career Karma main page. <\/p>\n\n\n\n<p><em>google.com<\/em> is at the top of the stack. The google.com site will be the first item our browser removes from the stack. This happens when we press the back arrow button to return to the last page we visited.<\/p>\n\n\n\n<p>Another example of a stack in action is a pile of books. (Think of a <em>stack<\/em> of books!) If you want to look through a pile of books, you\u2019ll go from the top book down. The last book you added to the stack will be the first one you look at\u2014in other words, LIFO.<\/p>\n\n\n\n<p>When you\u2019re working with stacks, the last item in the list is considered at the <em>top of the stack<\/em>. To use our website example from above, google.com was the most recent site we visited, so it is at the top of our stack.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Create a Java Stack<\/h2>\n\n\n\n<p>Before we create a stack in Java, we have to import the java.util.Stack package. This package stores the stack data structure that we will use in this tutorial.<\/p>\n\n\n\n<p>Here\u2019s how to import the stack data structure:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.Stack;<\/pre><\/div>\n\n\n\n<p>Now that we\u2019ve imported the stack package, we can create a Java stack. Here\u2019s the syntax we used to create a stack in Java:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Stack&lt;DataType&gt; stack_name = new Stack&lt;&gt;();<\/pre><\/div>\n\n\n\n<p>Here are the main components of the Java stack syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Stack<\/strong> tells our program that we want to declare a stack.<\/li><li><strong>DataType<\/strong> is the type of data our stack will store.<\/li><li><strong>stack_name<\/strong> is the name of our stack.<\/li><li><strong>new Stack&lt;&gt;();<\/strong> initializes a new Java stack.<\/li><\/ul>\n\n\n\n<p>If we wanted to create a stack called <code>books<\/code> that stores strings, we could use this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Stack&lt;String&gt; books = new Stack&lt;&gt;();<\/pre><\/div>\n\n\n\n<p>We have created a new stack. This stack stores data in the last-in, first-out order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>A Note on the Deque Class<\/strong><\/h3>\n\n\n\n<p>It\u2019s worth noting that some programmers prefer to use the<a href=\"https:\/\/careerkarma.com\/blog\/python-deque-queue\/\"> deque class<\/a> instead of stack in Java.<\/p>\n\n\n\n<p>Deques are double-ended queues. The advantage of a deque over a stack is that you can add and remove items from both ends of a deque. You cannot do this with a stack.<\/p>\n\n\n\n<p>If you use stack-specific methods (discussed below), you\u2019ll have to commit to using the stack class in your code. This can make it more difficult to scale a program. That said, stack still has a wide variety of uses in Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Stack Methods<\/h2>\n\n\n\n<p>The Java stack class includes a number of methods that you can use to manipulate data stored within a stack. These methods can be divided into the following two categories:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Stack methods inherited from the vector class.<\/strong> The first methods stack offers are those inherited from the vector class. If you\u2019re looking to learn more about these methods, research the \u201c<a href=\"https:\/\/careerkarma.com\/blog\/java-vector\/\">Java vector class<\/a>\u201d.&nbsp;&nbsp;<\/li><\/ol>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Stack methods that are unique to stack.<\/strong> Stack also offers five additional methods that are unique to the class. We\u2019ll discuss them below. They are:<\/li><\/ol>\n\n\n\n<ul class=\"wp-block-list\"><li>push()<\/li><li>pop()<\/li><li>peek()<\/li><li>empty()<\/li><li>search()<\/li><\/ul>\n\n\n\n<p>The push() and pop() operations are arguably the most commonly used. They let you add and remove items from a stack respectively.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Add Item to a Java Stack: push()<\/h3>\n\n\n\n<p>The <em>push()<\/em> method adds an item to a Java stack.<\/p>\n\n\n\n<p><em>push()<\/em> accepts one parameter: the item you want to add to your stack. For instance, suppose we are creating a stack that stores all the book titles in a library\u2019s fiction section. We could use this code to add the first three book titles to the stack:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.Stack;\n\nclass AddBooks {\n\tpublic static void main(String[] args) {\n\t\tStack&lt;String&gt; books = new Stack&lt;&gt;();\n\n\t\tbooks.push(&quot;Pride and Prejudice&quot;);\n\t\tbooks.push(&quot;Nineteen Eighty-Four&quot;);\n\t\tbooks.push(&quot;The Great Gatsby&quot;);\n\n\t\tSystem.out.println(books);\n\t}\n}<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>[Pride and Prejudice, Nineteen Eighty-Four, The Great Gatsby]<\/pre><\/div>\n\n\n\n<p>First, we import the Java stack module. Then we declare a class\u2014called <em>AddBooks<\/em>\u2014that stores the code for our program.\n\n<\/p>\n\n\n\n<p>On the next line, we initialize a new stack\u2014called <em>books<\/em>\u2014that can store string values. We then use the <em>push()<\/em> method to add three books titles to our stack: <em>Pride and Prejudice<\/em>, <em>Nineteen Eighty-Four<\/em>, and <em>The Great Gatsby<\/em>. Finally, we print out the book titles in our <em>books<\/em> stack to the console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Remove Item from a Java Stack: pop()<\/h3>\n\n\n\n<p>The <code>pop()<\/code> method removes an element from the top of a stack. This method returns the element that you removed from the stack.<\/p>\n\n\n\n<p>Suppose we wanted to remove from our stack the entry for <em>The Great Gatsby<\/em>, which was the last book title we entered. We could add the following code to our program above to accomplish this task:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class RemoveBooks {\n\tpublic static void main(String[] args) {\n\t\tStack&lt;String&gt; books = new Stack&lt;&gt;();\n\n\t\tbooks.push(&quot;Pride and Prejudice&quot;);\n\t\tbooks.push(&quot;Nineteen Eighty-Four&quot;);\n\t\tbooks.push(&quot;The Great Gatsby&quot;);\n\n\t\tString removed_book = books.pop();\n\t\tSystem.out.println(&quot;Books: &quot; + books);\n\t\tSystem.out.println(&quot;Removed book: &quot; + removed_book);\n\t}\n}<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Books: [Pride and Prejudice, Nineteen Eighty-Four]\nRemoved book: The Great Gatsby<\/pre><\/div>\n\n\n\n<p>In this example, our code removes the item on the top of the stack.<\/p>\n\n\n\n<p>The item at the top of the stack is <em>The Great Gatsby. <\/em>Then, our program prints out the revised list of books preceded by <em>Books:<\/em> to the console. The title of the removed book preceded by <em>Removed book:<\/em> is also displayed on the console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Retrieve First Object in Stack: peek()<\/h3>\n\n\n\n<p>When you\u2019re working with stacks, you may want to retrieve the item at the top of the stack. That\u2019s where the <em>peek()<\/em> method comes in. <em>peek()<\/em> accepts no parameters. It <em>peeks<\/em> at the top of the stack and returns the item it finds.<\/p>\n\n\n\n<p>Suppose we want to find out which item is at the top of our stack, now that we have removed <em>The Great Gatsby<\/em>. We can use the following code to do so:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.Stack;\n\nclass FindTopBook {\n\tpublic static void main(String[] args) {\n\t\tStack&lt;String&gt; books = new Stack&lt;&gt;();\n\n\t\tbooks.push(&quot;Pride and Prejudice&quot;);\n\t\tbooks.push(&quot;Nineteen Eighty-Four&quot;);\n\n\t\tString top_book = books.peek();\n\n\t\tSystem.out.println(&quot;Book at top of stack: &quot; + top_book);\n\t}\n}<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Book at top of stack: Nineteen Eighty-Four.<\/pre><\/div>\n\n\n\n<p>Our stack contains two items. <em>Nineteen Eighty-Four<\/em> is at the top of the stack, so when we use the <em>peek()<\/em> method, our program returns that book title.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Check if a Stack is Empty: empty()<\/h3>\n\n\n\n<p>The <em>empty()<\/em> method determines whether a stack is empty.<\/p>\n\n\n\n<p>For instance, suppose we want to check whether our <em>books<\/em> stack is empty. We have been playing around with the data in our stack. Now we are not sure if the stack contains any more book titles.<\/p>\n\n\n\n<p>We could use the following code to check if our stack is empty:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.Stack;\n\nclass CheckBooksEmpty {\n\tpublic static void main(String[] args) {\n\t\tStack&lt;String&gt; books = new Stack&lt;&gt;();\n\n\t\tbooks.push(&quot;Pride and Prejudice&quot;);\n\t\tbooks.push(&quot;Nineteen Eighty-Four&quot;);\n\n\t\tString is_empty = books.empty();\n\n\t\tSystem.out.println(&quot;Is the book stack empty? &quot; + is_empty);\n\t}\n}<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Is the book stack empty? false<\/pre><\/div>\n\n\n\n<p>Our <em>books<\/em> stack contains two values, so it is not empty. Thus, <em>books.empty()<\/em> returns: false.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Search for an Element: search()<\/h3>\n\n\n\n<p>The <em>search()<\/em> method searches for an element in a <em>stack.<\/em><\/p>\n\n\n\n<p><em>search()<\/em> accepts one parameter: the name of the item for which you want to search. It returns that item\u2019s position in the stack.<\/p>\n\n\n\n<p>Suppose we wanted to find out the position of <em>Pride and Prejudice<\/em> in our book titles stack. We could do so using this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.util.Stack;\n\nclass FindPrideBookPosition {\n\tpublic static void main(String[] args) {\n\t\tStack&lt;String&gt; books = new Stack&lt;&gt;();\n\n\t\tbooks.push(&quot;Pride and Prejudice&quot;);\n\t\tbooks.push(&quot;Nineteen Eighty-Four&quot;);\n\n\t\tString find_book = books.search(&quot;Pride and Prejudice&quot;);\n\n\t\tSystem.out.println(&quot;Position of 'Pride and Prejudice': &quot; + find_book);\n\t}\n}<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Position of 'Pride and Prejudice': 1. <\/pre><\/div>\n\n\n\n<p>The first item in a stack has the position number <em>1<\/em>,. Because <em>Pride and Prejudice<\/em> is first item in the stack, our program returns 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The Java stack class is used to create stacks with a LIFO structure. This tutorial discussed the basics of Java stacks and how you can create a stack. We also talked through the five methods used to retrieve and manipulate the contents of a stack.<\/p>\n\n\n\n<p>Now you\u2019re equipped with the knowledge you need to work with stacks like a professional Java developer. You can learn more about the Java programming language by reading our <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-java\/\">How to Learn Java guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"A Java stack is a last-in, first-out data structure. The first item removed from a stack is the item last added to the stack. A stack data structure adds new items to the end of the stack. Java stacks extend the Vector class. In programming, a stack is a last-in, first-out data structure used to&hellip;","protected":false},"author":240,"featured_media":13631,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17289],"tags":[],"class_list":{"0":"post-13630","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Java","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Java Stack Class: The Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"You can use the \u201cstack\u201d class to create a last-in, first-out stack in Java. On Career Karma, learn how to use Java\u2019s stack class in your code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/java-stack\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Stack Class\" \/>\n<meta property=\"og:description\" content=\"You can use the \u201cstack\u201d class to create a last-in, first-out stack in Java. On Career Karma, learn how to use Java\u2019s stack class in your code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/java-stack\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-20T19:27:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:03:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"681\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"Java Stack Class\",\"datePublished\":\"2020-10-20T19:27:21+00:00\",\"dateModified\":\"2023-12-01T12:03:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/\"},\"wordCount\":1469,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/java-stack\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/java-stack\/\",\"name\":\"Java Stack Class: The Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg\",\"datePublished\":\"2020-10-20T19:27:21+00:00\",\"dateModified\":\"2023-12-01T12:03:14+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"You can use the \u201cstack\u201d class to create a last-in, first-out stack in Java. On Career Karma, learn how to use Java\u2019s stack class in your code.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/java-stack\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg\",\"width\":1020,\"height\":681},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/java-stack\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\/\/careerkarma.com\/blog\/java\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Java Stack Class\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Java Stack Class: The Complete Guide | Career Karma","description":"You can use the \u201cstack\u201d class to create a last-in, first-out stack in Java. On Career Karma, learn how to use Java\u2019s stack class in your code.","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:\/\/careerkarma.com\/blog\/java-stack\/","og_locale":"en_US","og_type":"article","og_title":"Java Stack Class","og_description":"You can use the \u201cstack\u201d class to create a last-in, first-out stack in Java. On Career Karma, learn how to use Java\u2019s stack class in your code.","og_url":"https:\/\/careerkarma.com\/blog\/java-stack\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-10-20T19:27:21+00:00","article_modified_time":"2023-12-01T12:03:14+00:00","og_image":[{"width":1020,"height":681,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/java-stack\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/java-stack\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"Java Stack Class","datePublished":"2020-10-20T19:27:21+00:00","dateModified":"2023-12-01T12:03:14+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-stack\/"},"wordCount":1469,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/java-stack\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/java-stack\/","url":"https:\/\/careerkarma.com\/blog\/java-stack\/","name":"Java Stack Class: The Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-stack\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-stack\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg","datePublished":"2020-10-20T19:27:21+00:00","dateModified":"2023-12-01T12:03:14+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"You can use the \u201cstack\u201d class to create a last-in, first-out stack in Java. On Career Karma, learn how to use Java\u2019s stack class in your code.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/java-stack\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/java-stack\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/java-stack\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/three-woman-in-front-of-laptop-computer-1181233.jpg","width":1020,"height":681},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/java-stack\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/careerkarma.com\/blog\/java\/"},{"@type":"ListItem","position":3,"name":"Java Stack Class"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=13630"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13630\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/13631"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=13630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=13630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=13630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}