Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

empty() vs isEmpty() in Java Stack class

Tags:

java

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

like image 943
Aafreen Sheikh Avatar asked Jul 28 '14 00:07

Aafreen Sheikh


People also ask

What is isEmpty () condition in stack?

isEmpty() method in Java is used to check and verify if a Stack is empty or not. It returns True if the Stack is empty else it returns False. Syntax: Stack.isEmpty() Parameters: This method does not take any parameter.

What is the difference between isEmpty and empty?

Both methods are used to check for blank or empty strings in java. The difference between both methods is that isEmpty() method returns true if, and only if, string length is 0.

Does empty method stack Java?

empty() method in Java is used to check whether a stack is empty or not. The method is of boolean type and returns true if the stack is empty else false. Parameters: The method does not take any parameters. Return Value: The method returns boolean true if the stack is empty else it returns false.


1 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.

like image 139
Adrian Shum Avatar answered Sep 22 '22 20:09

Adrian Shum