The add(Object) method of Stack Class appends the specified element to the end of this Stack.
Syntax:
boolean add(Object element)
Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the Stack.
Return Value: This method returns True after successful execution, else False.
Below program illustrates the working of java.util.Stack.add(Object element) method:
Example 1:
// Java code to illustrate boolean add(Object element) import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<String> stack = new Stack<String>(); // Use add() method // to add elements in the Stack stack.add("Geeks"); stack.add("for"); stack.add("Geeks"); stack.add("10"); stack.add("20"); // Output the present Stack System.out.println("The Stack is: " + stack); // Adding new elements to the end stack.add("Last"); stack.add("Element"); // Printing the new Stack System.out.println("The new Stack is: " + stack); } } |
The Stack is: [Geeks, for, Geeks, 10, 20] The new Stack is: [Geeks, for, Geeks, 10, 20, Last, Element]
Example 2:
// Java code to illustrate // boolean add(Object element) import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<Integer> stack = new Stack<Integer>(); // Use add() method // to add elements in the Stack stack.add(10); stack.add(20); stack.add(30); stack.add(40); stack.add(50); // Output the present Stack System.out.println("The Stack is: " + stack); // Adding new elements to the end stack.add(100); stack.add(200); // Printing the new Stack System.out.println("The new Stack is: " + stack); } } |
The Stack is: [10, 20, 30, 40, 50] The new Stack is: [10, 20, 30, 40, 50, 100, 200]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Stack capacity() method in Java with Example
- Stack contains() method in Java with Example
- Stack equals() method in Java with Example
- Stack get() method in Java with Example
- Stack indexOf() method in Java with Example
- Stack containsAll() method in Java with Example
- Stack clear() method in Java with Example
- Stack copyInto() method in Java with Example
- Stack insertElementAt() method in Java with Example
- Stack isEmpty() method in Java with Example
- Stack elementAt() method in Java with Example
- Stack clone() method in Java with Example
- Stack ensureCapacity() method in Java with Example
- Stack firstElement() method in Java with Example
- Stack hashCode() method in Java with Example
- Stack indexOf(Object, int) method in Java with Example
- Stack iterator() method in Java with Example
- Stack elements() method in Java with Example
- Stack addElement(E) method in Java with Example
- Stack addAll(int, Collection) method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

