40

I am trying to use Stack, but I am slightly confused by the terminology.

I find that Stack class has only push(E e) as per Java doc.

And has add(E e) and addAll(Collection<? extends E> c) as a inherited method from Vector class.

Do they have the same functionality or is it better to use push(...) to insert elements to the Stack object.

In other words, will I encounter any issues if I use add(...) instead of push(...)?

darijan
  • 9,725
  • 25
  • 38
Kalyanaraman Santhanam
  • 1,371
  • 1
  • 18
  • 30

10 Answers10

30

Kalyanaraman Santhanam:

Edit: Will I encounter any issues if I use add(...) instead of push(...)?

Definitly, you will not encounter any issues, because add is part of List interface as well as the Stack, but you should to notice the further readability of your code and your intentions in it by other programmers. push method will give them a clue that they're using the Stack object, they will know certantly what to expect from. Also notice that push has different return value than add (the former has "pushed object" type and the latter just a boolean response)

Community
  • 1
  • 1
rook
  • 5,880
  • 4
  • 39
  • 51
  • Example : `Deque` interface has `void addFirst(E e);` and `void push(E e);` which are similar in behaviour including the return type. from the java doc it says **

    This method is equivalent to {@link #addFirst}.**

    – amarnath harish Jun 02 '18 at 07:58
8

The only difference is the return type

System.out.println(stack.push("1"));

Output : 1

System.out.println(stack.add("2"));

Output : true

But it is recommended to use Push method for Stack

Constantin Beer
  • 5,447
  • 6
  • 25
  • 43
SDC
  • 161
  • 1
  • 11
4

(sorry if my english is bad, i'm from MX).

Theorically, is the same thing, because add is a method of all the generic class... But is very recommendable to use push, because you are using a stack and if you use "push" method instead of "add", maybe you'll understand a little bit more...

3

They are the same.

From the JavaDoc:

Pushes an item onto the top of this stack. This has exactly the same effect as:

addElement(item)
wchargin
  • 15,589
  • 12
  • 71
  • 110
Frank
  • 16,476
  • 7
  • 38
  • 51
3

As everyone said, it has same effect with "push". Some may ask, but why? I recently read this subject, and it was saying it is a violation of principle "principle of least astonishment". So it is like a design flaw of API, not a major issue. This teaches us, no software, framework, API, etc.. are perfect, everything has its own flaws.

LazyX
  • 177
  • 2
  • 10
1

If you are using a Stack then you should use push() as this is the standard way to add elements onto a stack (due to the idea of the data structure of a Stack). This means that the "top of the stack" is the item you've just push()ed.

You should only ever add and remove from the top of a Stack and if you think you will need other ways of adding data (add to middle or end) then I would advise against using a Stack as it will make your code harder to understand.

Edit: (reflecting question edit) As I've mentioned, I would not expect to see add() if I was dealing with a Stack. add() is not the standard syntax for using a Stack, push() is. I would recommend only using the functions defined in the main body of the javadoc unless you specifically need those that are inherited.

ryaanwells
  • 280
  • 1
  • 2
  • 12
1

Yeah ,I can't find much difference between push() and add() method on using them.But preferred to use push() while using Stack

RAJNISH YADAV
  • 141
  • 1
  • 6
1

What i recently encountered while attempting a coding question (actually the situation which led me to search the same question) is : My code was exceeding time limit when i used stack.add(string object). While it resolved successfully as i changed .add to .push i.e. stack.push(string object). Literally i had changed nothing else (except .add -> .push), and my code executed in time!

MGupt
  • 31
  • 3
0

It is all about inheritance issues, if it is a Stack you should use push() to be clear and follow to the model. But, in fact from Java SE spec:

Pushes an item onto the top of this stack. This has exactly the same effect as: addElement(item)

rook
  • 5,880
  • 4
  • 39
  • 51
0

From my analysis, I could see no difference between the output of the add and push methods. Please refer the code and the output below..

import java.util.Stack;

public class StackDemo {

    static Stack stack = new Stack();

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        add(10);
        add(20);
        add(30);
        print();
        push(40);
        print();
        pop();
        print();
        push(50);
        print();

    }

    static void print() {
        System.out.println(stack);
    }

    static void add(int item) {
        stack.add(item);
    }

    static void push (int item) {
        stack.push(item);
    }

    static void peek() {
        System.out.println(stack.peek());
    }

    static int pop() {
        return stack.pop();
    }
}
*
[10, 20, 30]
[10, 20, 30, 40]
[10, 20, 30]
[10, 20, 30, 50]
*