GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Java Stack push() Method

Java.util package tutorial
  • Java.util - Home
  • Java.util - ArrayDeque
  • Java.util - ArrayList
  • Java.util - Arrays
  • Java.util - BitSet
  • Java.util - Calendar
  • Java.util - Collections
  • Java.util - Currency
  • Java.util - Date
  • Java.util - Dictionary
  • Java.util - EnumMap
  • Java.util - EnumSet
  • Java.util - Formatter
  • Java.util - GregorianCalendar
  • Java.util - HashMap
  • Java.util - HashSet
  • Java.util - Hashtable
  • Java.util - IdentityHashMap
  • Java.util - LinkedHashMap
  • Java.util - LinkedHashSet
  • Java.util - LinkedList
  • Java.util - ListResourceBundle
  • Java.util - Locale
  • Java.util - Observable
  • Java.util - PriorityQueue
  • Java.util - Properties
  • Java.util - PropertyPermission
  • Java.util - PropertyResourceBundle
  • Java.util - Random
  • Java.util - ResourceBundle
  • Java.util - ResourceBundle.Control
  • Java.util - Scanner
  • Java.util - ServiceLoader
  • Java.util - SimpleTimeZone
  • Java.util - Stack
  • Java.util - StringTokenizer
  • Java.util - Timer
  • Java.util - TimerTask
  • Java.util - TimeZone
  • Java.util - TreeMap
  • Java.util - TreeSet
  • Java.util - UUID
  • Java.util - Vector
  • Java.util - WeakHashMap
  • Java.util - Interfaces
  • Java.util - Exceptions
  • Java.util - Enumerations
  • Java.util Useful Resources
  • Java.util - Useful Resources
  • Java.util - Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Java Stack push() Method



Previous
Quiz
Next

Description

The Java Stack push(Object item) method is used to push an item onto the top of this stack.

Declaration

Following is the declaration for java.util.Stack.push() method.

public Object push(Object item)

Parameters

item − This is the item to be pushed onto this stack.

Return Value

The method call returns the item argument.

Exception

NA

Push Integer on top of Stack Example

The following example shows the usage of Java Stack push() method to add the integers onto the top of the stack. In this example, we've created a Stack object of Integers. Then we've added few integers to the stack and printed the stack elements.

package com.tutorialspoint;

import java.util.Stack;

public class StackDemo {
   public static void main(String args[]) {

      // creating stack
      Stack<Integer> st = new Stack<>();

      // populating stack
      st.push(10);
      st.push(20);
      st.push(30);

      // checking elements
      System.out.println("Elements in the stack: "+st);
   }     
}

Output

Let us compile and run the above program, this will produce the following result.

Elements in the stack: [10, 20, 30]

Push String on top of Stack Example

The following example shows the usage of Java Stack push() method to add the strings onto the top of the stack. In this example, we've created a Stack object of Strings. Then we've added few strings to the stack and printed the stack elements.

package com.tutorialspoint;

import java.util.Stack;

public class StackDemo {
   public static void main(String args[]) {

      // creating stack
      Stack<String> st = new Stack<>();

      // populating stack
      st.push("Java");
      st.push("Source");
      st.push("code");

      // checking elements
      System.out.println("Elements in the stack: "+st);
   }     
}

Output

Let us compile and run the above program, this will produce the following result.

Elements in the stack: [Java, Source, code]

Push Object on top of Stack Example

The following example shows the usage of Java Stack push() method to add the student object onto the top of the stack. In this example, we've created a Stack object of Student objects. Then we've added few students to the stack and printed the stack elements.

package com.tutorialspoint;

import java.util.Stack;

public class StackDemo {
   public static void main(String args[]) {

      // creating stack
      Stack<Student> st = new Stack<>();

      // populating stack
      st.push(new Student(1, "Julie"));
      st.push(new Student(2, "Robert"));
      st.push(new Student(3, "Adam"));

      // checking elements
      System.out.println("Elements in the stack: "+st);
   }     
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result.

Elements in the stack: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_stack.htm
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.