java.lang.String#contentEquals(CharSequence) suboptimal?

Alexander Veit alexander.veit at gmx.net
Mon Jan 4 17:08:04 UTC 2010


 Hi Jesús,

> String.equals already performs that instanceof comparison, 
> and is one of the first to be inlined by the hotspot. What I 
> would do is to turn it... "this.equals(cs)" instead of 
> "cs.equals(this)".

That's true, but given cs is a String with length equal to this, cs.equals(this) nevertheless may return false, and

 if (cs.equals(this))
  return true;

will not return. The remaining code performs the comparison a second time in this case.

> [...]
> Anyway, does anyone know if the 
> contentEquals method is actually used out there? (at least it 
> isn't inside the JDK source code).

I use it ;-) It is useful to prevent from duplicate memory allocation in quite some cases.


-- 
Cheers,
Alex




More information about the core-libs-dev mailing list