The moose likes Java in General and the fly likes Comparing Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing Strings" Watch "Comparing Strings" New topic

Comparing Strings

Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
Which the fastest way to compare two strings
S1.equals(S2) or S1.intern()==S2.intern()
or is there any other methods which give better performance

I just want to know is there any body call my bean's Getter and Setter methods with "Please" in front - My favorite quip from Bugzilla
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
My understanding is that, since S1.intern()==S2.intern() is actually comparing references, it's faster.


So much trouble in the world -- Bob Marley
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Well, the == part of the operation is certainly faster if the intern() parts have already been done. But I'm sure an equals() will be faster than two intern() calls plus an ==, and it's probably faster than a single intern() plus == as well. So this approach really makes sense only if you expect to do comparisons more than once for each value, so you only pay the overhead of interning once, and reap its subsequent benefits on each compare. But I'm too lazy to do actual tests of this theory right now, so you should probably test both methods under various conditions to see which is really faster.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Comparing Strings
It's not a secret anymore!