• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

which is better to use when comparing Strings

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic