Vaibhav Bajpai
Vaibhav Bajpai

Reputation: 16774

equals(...) and equalsIgnoreCase(...)

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?

Upvotes: 15

Views: 23598

Answers (6)

Venkat
Venkat

Reputation: 21480

equalIgnoreCase() is used for ignore the Case sensitive of our String. But the equals() is only returns true, while be same case of string

ex,

String value="java";
if(value.equals("JAva")
{
    System.out.println("Return True");
}
else
{
    System.out.println("Return False");
}

Ans: Returns False

but the other one is,

if(value.equalIgnoreCase("JAva")
{
    System.out.println("Return True");
}
else
{
    System.out.println("Return False");
}

Ans: Returns True

Upvotes: 3

MauriceL
MauriceL

Reputation: 435

The main test when overridng a method with additional parameters is that I would expect any method override to do exactly the same thing as the method it's overriding. Equals(), being derived from Object has a contract it must follow. Two objects that are equal() should have identical hashcodes. I don't think two objects that are case insensitive equal should have the same hashcode, so I believe overriding equal here is the wrong thing to do.

Upvotes: 1

user256717
user256717

Reputation:

I think they just chose one of the alternatives. .NET chose the other. StringComparison.InvariantCultureIgnoreCase etc.

Definitely what you are suggesting and [even better what] .NET implemented would have been more flexible for different cultures etc. In fact I don't even know what culture they use in this ignore case. I guess Current culture.

Upvotes: 0

Péter Török
Péter Török

Reputation: 116256

The method equals() is inherited from Object, 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 other equals() without downcasting your object to String first.

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

if (string1.equalsIgnoreCase(string2)) ...

is more readable, thus less error-prone than

if (string1.equals(string2, true)) ...

Of course, in your own classes you are free to add an equals() with different signature (on top of the standard equals(), that is).

Upvotes: 19

missingfaktor
missingfaktor

Reputation: 92026

It's absolutely possible to do what you are suggesting but the language designers chose to go the other way and hence we have equalsIgnoreCase(otherString) instead of say equals(otherString, StringConstants.IGNORE_CASE) or equals(otherString, true).

Upvotes: 2

Roman
Roman

Reputation: 66156

Because equals() method is inherited from Object.

If they did it as you suggest then we would have something like this:

public final class String {

    public boolean equals () { ... }

    public boolean equals (boolean ignoreCase) { ... }

} 

And without reading documentation it would be impossible to understand what method equals() (which without parameter) do.

Upvotes: 1

Related Questions