• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Performance factor in using equals Vs comareTo

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[color=violet]Hi,
As we know that equals() and compareTo() is used to evaluate String Object is equal or same, my question here is "Which one is better choice when it comes to performance and why?"

Note: This question is keeping in context of String class
[/color]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suhas,

equals() is better choice to test equalitity of Strin object. Also, its faster that compareTo().

compareTo() compares two strings lexicographically.The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.

equals() compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

I hope this will help!

Thanks,
Samir
 
Suhas Madap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Samir,
Thanks a lot, your response logically make sense. Even I felt the same.
 
Marshal
Posts: 71620
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should ignore considerations about performance at this stage. There is probably no significant performance difference between the two; you can investigate the source code by unzipping the "src.zip" file in your Java installation folder. Have a look at this article by Brian Goetz and see what he says about performance. You should use methods for their designed purpose. If there is an equals() method use that for equality and if there is a compare/compareTo method use that for comparisons.

There are a few possible exceptions however. The BigDecimal equals method returns false if you present 1.0 with 1.00, so you would have to use the compareTo method and == 0.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic