Why do we have equals() and equalsIgnoreCase() as two different methods, when equals() could have been overloaded with a special ignoreCase argument to provide equalsIgnoreCase() functionality?
Why do we have equals() and equalsIgnoreCase() as two different methods, when equals() could
Share
The method
equals()is inherited fromObject, so its signature should not be changed.equals()can often be used without actually knowing the concrete class of the object, e.g. when iterating through a collection of objects (especially before Java 5 generics). So then you wouldn’t even see the otherequals()without downcasting your object toStringfirst.This was a design choice from the creators of Java to make the idiom of using
equals()usable exactly the same way for all objects.Moreover, IMO
is more readable, thus less error-prone than
Of course, in your own classes you are free to add an
equals()with different signature (on top of the standardequals(), that is).