Java Stack push() vs add()
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)
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