• Post Reply Bookmark Topic Watch Topic
  • New Topic

which is better to use when comparing Strings  RSS feed

 
Cyrus Serrano
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

im fond of using equals method of the String class when comparing strings. but a friend of mine says its much better to use a compareTo(). Although i know that the compareTo comapares per character, as i remember.

What do you think?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compareTo returns 0 when strings are equal, something else when they are not. So you can use it to do the job of equals()

if ( a.equals( b ) )
if ( a.compareTo( b ) == 0 )

Besides being less typing and maybe some insignificant bit quicker, equals() communicates better that we're just checking for equality.

You may find some custom classes outside the Java library that correctly implement one of these but not the other. For example, I just implemented compareTo on some classes that I wanted to put in a TreeSet, but didn't implement equals(). That could surely come back to bite me!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparison and equality are not required to use the same fields, so if you want to look for equality then equals should always be the one to go with.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cyrus Serrano:
...a friend of mine says its much better to use a compareTo()...

Did your friend say why it's "much better"?

Isn't equality testing usually used as a condition for doing something else? In that case, the boolean returned by equals seems to make more sense than the int returned by compareTo, which would then need to be checked for its own (in)equality to zero.

If you're comparing in order to sort, then compareTo makes more sense. But if you're just testing equality...
[ September 27, 2007: Message edited by: marc weber ]
 
Cyrus Serrano
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the quick replies.. i appreciate all the comments.

thats what im thinking, if just for comparing strings why the hussle of performing another condition to check on using the compareTo rather than the plain old equals method.

thanks..
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparison and equality are not required to use the same fields, so if you want to look for equality then equals should always be the one to go with.

That's true, because we can write either or both methods and get them as wrong as any other method Do you know any examples where equals() and compareTo()==0 give different results? I'd be curious about how that's documented and explained.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!