This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Performance and the fly likes which is faster equals() or equalsIgnoreCase() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Performance
Bookmark "which is faster equals() or equalsIgnoreCase()" Watch "which is faster equals() or equalsIgnoreCase()" New topic

which is faster equals() or equalsIgnoreCase()

punna Kumar
Ranch Hand

Joined: Feb 28, 2005
Posts: 80
could u plz explain the defference between the equals() and equalsIgnoreCase() methods in String class,and explain which is the better for performance.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Performance is not the major differentiation between the two methods, as they do different things. equalsIgnoreCase ignores the case of the two strings, just like its name suggests it should do.

"abc".equals("abc") --> true
"ABC".equals("abc") --> false
"def".equals("abc") --> false

"abc".equalsIgnoreCase("abc") --> true
"ABC".equalsIgnoreCase("abc") --> true
"def".equalsIgnoreCase("abc") --> false

If you're still interested in their relative performance, I would guess that equals is faster, because it only needs to compare the individual characters for equality, and not consider up-/downcasing it and then comparing that as well. That shouldn't make a big difference, though. The source of java.lang.String will tell you for sure.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
To be fully sure, you'd have to write a Benchmark. Writing a good one is an art, though. And it still wouldn't tell you wether it actually had any significant impact on the system you are developing - for that you needed to run it through a profiler.

"You don't need to care" is a quite good working hypothesis, though...
[ July 06, 2005: Message edited by: Ilja Preuss ]

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
For most purposes the question is purely academic and could also change from one version of the jvm to another. For most purposes both are very fast. Plus, as another poster said they return different results, so you just can't swap them.

Here are some results I found using google on the matter. As, I recall Jack Shirazi's "Java Performance Tuning" book dicusses the matter too.


String.equalsIgnoreCase is a lot faster than String.equals when the majority of the compared data have different lengths because equalsIgnoreCase first checks the length of the two strings.


compareTo is two to three orders of magnitude faster than either of the equals methods. In my study - not shown here - I have found that the method call equalsIgnoreCase of the class String is a lot faster than the method call equals of the same class when the majority of the compared data have different lengths. There is a good reason for this, of course. The equalsIgnoreCase first checks the length of the two strings

[ July 08, 2005: Message edited by: steve souza ]
[ July 08, 2005: Message edited by: steve souza ] - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Excellent point, Steve. Note, though, that (as you said!) these results can change. I just looked at the source for 1.4.2_08, and String.equals() does indeed check the lengths first, and equalsIgnoreCase() does a lot more work than equals() does, so it looks as though equals() is now the faster method.

[Jess in Action][AskingGoodQuestions]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: which is faster equals() or equalsIgnoreCase()
It's not a secret anymore!