The add(Object) method of Stack Class appends the specified element to the end of this Stack.
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:
The Stack is: [Geeks, for, Geeks, 10, 20] The new Stack is: [Geeks, for, Geeks, 10, 20, Last, Element]
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 Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.