Empty() vs isEmpty() in Java Stack Class

Why does Stack in Java have an empty() method along with the usual isEmpty()? All abstract classes that Stack extends have an isEmpty() method.

4

2 Answers

I believe OP's question is more on : why there are duplicated methods, given empty() and isEmpty() are doing the same thing?

If you take a closer look, in Vector, Stack and HashTable, there are more examples of methods doing similar thing with different names.

Here is the brief history:

At the time of JDK 1.0, there was no "Collection" framework in Java. Stack, Vector, HashTable were some of the basic data structures provided by Java.

Later in JDK 1.2, Collection framework was added to JDK, and standard interfaces (like List, Map) were introduced.

However in these new standard collection interfaces, methods were named in a different convention. The change in naming convention was most probably influenced by Java Bean standard introduced also in JDK 1.2. These method names were different from those in old Stack, Vector and HashTable classes. For example, it was named empty() in original class but was named isEmpty() of Collection interface.

In order to make Stack, Vector and HashTable compatible with Collection framework, Stack, Vector and HashTable has implemented its corresponding Collection interfaces. On the same time, old methods were kept for the sake of backward compatibility.

Hence the "duplicated" methods you see now.

5

both return boolean only difference is isEmpty is synchronized points to vector whereas empty is not synchronized points to stack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest