When testing for equality of String
's in Java I have always used equals()
because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. However, a colleague of mine recently told me had been taught to use compareTo() == 0
instead of equals()
. This feels unnatural (as compareTo()
is meant to provide an ordering and not compare for equality) and even somewhat dangerous (because compareTo() == 0
does not necessarily imply equality in all cases, even though I know it does for String
's) to me.
He did not know why he was taught to use compareTo()
instead of equals()
for String
's, and I could also not find any reason why. Is this really a matter of personal taste, or is there any real reason for either method?
When comparing for equality you should use
equals()
, because it expresses your intent in a clear way.compareTo()
has the additional drawback that it only works on objects that implement theComparable
interface.This applies in general, not only for Strings.
A difference is that
"foo".equals((String)null)
returns false while"foo".compareTo((String)null) == 0
throws a NullPointerException. So they are not always interchangeable even for Strings.compareTo
has do do more work if the strings have different lengths.equals
can just return false, whilecompareTo
must always examine enough characters to find the sorting order.Equals -
1- Override the GetHashCode method to allow a type to work correctly in a hash table.
2- Do not throw an exception in the implementation of an Equals method. Instead, return false for a null argument.
3-
Successive invocations of x.Equals(y) return the same value as long as the object referenced by x and y are not modified.
4- For some kinds of objects, it is desirable to have Equals test for value equality instead of referential equality. Such implementations of Equals return true if the two objects have the same value, even if they are not the same instance.
For Example -
Output :-
while compareTo -
Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
It returns -
Less than zero - This instance precedes obj in the sort order. Zero - This instance occurs in the same position in the sort order as obj. Greater than zero - This instance follows obj in the sort order.
It can throw ArgumentException if object is not the same type as instance.
For example you can visit here.
So I suggest better to use Equals in place of compareTo.
compareTo()
not only applies to Strings but also any other object becausecompareTo<T>
takes a generic argumentT
. String is one of the classes that has implemented thecompareTo()
method by implementing theComparable
interface.(compareTo() is a method fo the comparable Interface). So any class is free to implement the Comparable interface.But
compareTo()
gives the ordering of objects, used typically in sorting objects in ascending or descending order whileequals()
will only talk about the equality and say whether they are equal or not.It appears that both methods pretty much do the same thing, but the compareTo() method takes in a String, not an Object, and adds some extra functionality on top of the normal equals() method. If all you care about is equality, then the equals() method is the best choice, simply because it makes more sense to the next programmer that takes a look at your code. The time difference between the two different functions shouldn't matter unless you're looping over some huge amount of items. The compareTo() is really useful when you need to know the order of Strings in a collection or when you need to know the difference in length between strings that start with the same sequence of characters.
source: http://java.sun.com/javase/6/docs/api/java/lang/String.html