java.util.Stack.push() Method


Advertisements


Description

The push(Object item) method is used to Pushes 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

Example

The following example shows the usage of java.util.Stack.push()

Live Demo
package com.tutorialspoint;

import java.util.*;

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

      // creating stack
      Stack st = new Stack();

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

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

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

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

java_util_stack.htm

Advertisements