[jdom-interest] Re: [jdom-commits] CVS update: jdom/src/java/org/jdom/input

Jason Hunter jhunter at acm.org
Sat Mar 2 15:54:18 PST 2002


> --- cvs at jools.org wrote:
> > Changed some string comparators from != to !.equals().  The former works
> > so long as the parser interns strings, and although Xerces and Crimson do,
> > not all parsers do.  So using !.equals() is safer.  Brad Huffman ran some
> > tests to see if this had much of a performance impact, and surprisingly
> > it didn't and in fact sometimes ran *faster*.  Below is his table, for
> > future reference.
> 
> Of-course it doesn't have much of a performance impact. Look at
> the first three lines of String.equals() :
> 
> if (this == anObject) {
>     return true;
> }
> 
> So, asking
>    if (str1.equals(str2))
> is just like asking
>    if (str1==str2 || str1.equals(str2))

Yes, in the rare event of an exact reference match it's fast.  But that
doesn't happen often.  So your != gets to go right to false while the
.equals() has to start looking at characters.  Luckily most strings
appear to differ on their initial chars and char indexing isn't too
slow.

-jh-



More information about the jdom-interest mailing list