I was reading through my text book on Java Programming...It showed two class methods compareTo() and equals()...It seems to be that there is no difference between this two...Can someone pls clear me on this..Thank You!

Recommended Answers

All 7 Replies

I was reading through my text book on Java Programming...It showed two class methods compareTo() and equals()...It seems to be that there is no difference between this two...Can someone pls clear me on this..Thank You!

compareTo(String anotherString) is used to compare if two Strings are equal and if they arn't tells your where they first are not equal.

equals(Object anObject) is also used to compare two Strings but will only tell you if the two strings are equal. It does not tell you where the two strings differ if they are different.

Regards,

Nate

I think you got it backwards.

compareTo() is used to compare Objects. It tests if THIS object is of the same type as the parameterized object. For instance,

String s = "t";
String s2 = "sdfsa";

s.compareTo(s2); ///would be true

.equals() compares the contents of the object. For instance,

String s = "t";
String s2 = "sfsdf";
s.equals(s2); // would return false.... "t" is not equal to "sdfdf"

commented: Thanks for catching my mistake. (b) +1

Actually, I think we're both wrong.

The compareTo(String anotherString) will actually tell you which string comes first based on lexicographical (a fancy word for an alphabetical) comparison.

Returns:
the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

So, the example that you gave server_crash:

String s = "t";
String s2 = "sdfsa";

s.compareTo(s2);

will actually return 1. Since t is lexicographically greater than s.

Whew. I guess i should read the java docs closer before answering a question.

Good catch server_crash.

Nate

compareTo is used for sequential placement of Objects. It's mainly useful in conjunction with sorted collections.
equals is used to simply check for logical equality.

In case of Strings compareTo will tell you which of the Strings being compared would come first in a case sensitive dictionary, equals would simply tell you if the two String references contain the same data.

But it's quite possible to define the methods in your own classes to have quite different behaviour (though not often recommended for the obvious reasons).
Most useful when defining them for your own classes is to compare only part of the fields in the objects, for example in case of objects representing database records you'd only want to compare the fields representing the primary key for the database table.

implementing compareTo is really only useful in combination with implementing the marker interface Comparable which is an indicator to sorted Collections that the compareTo method can be used to sort object instances of the class (if not a Comparator class is expected instead).

As a general point. Because Java uses objects for just about everything many comparisons and assignments use objects.

All objects are referenced from a variable of some type which points to the object. This allows other variables to point to the same object too which is what happens in many programs.

So the point is that when you compare variables you may want to know if they point to the same object OR if the two different objects have the same property or properties. If one was a copy of the other all properties would be equal but it would NOT be the same object.

When you use comparisons, even of Strings, bear in mind if you want to compare properties OR objects. If you don't you will have problems somewhere along the way!

System.out.println("enter a number:")

hi
there are not only two methods in java string class
you can find out all the methods of java string class by just typing
javap java.lang.String IN DOS SHELL
it will show a list of methods to you
compare to() this method is used to compare two string type values and give the ans in integer type.but if we talk about equals() method it checks whether two string type vales are equal to each other or not and shows ans in boolean type means(true or false)example:-

class first
{
public static void main(String []ar)
{
String name1="A";
String name2="B";
System.out.println(name1.compareTo(name2));
System.out.println(name1.equals(name2));
}
}
OUTPUT:-
-1
false
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.