What are the negative aspects of Java class Stack inheriting from Vector?

前端 未结 6 838
太阳男子
太阳男子 2020-11-30 04:35

By extending class Vector, Java’s designers were able to create class Stack quickly. What are the negative aspects of this use of inheritance, particularly for class Stack?<

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 04:43

    In addition to the main valid points mentioned above, another big problem with Stack inheriting from Vector is Vector is completely synchronized, so you get that overhead whether you need it or not (see StringBuffer vs. StringBuilder). Personally I tend to use ArrayDeque whenever I want a stack.

提交回复
热议问题