There's a heck of a lot wrong with intern().

1) People claim inefficiencies, without actually measuring them (see
other points I've made about permature optimisation)
2) The claim that performing an intern() reduces comparisons (because
you just do a pointer compare) is, in most cases, completely
irrelivant. When you perform an intern(), it has to search through all
previously intern()'d strings, and perform hashCode() and equals() on
each match it considers. Thus, a == b.intern() is equivalent to
a.hashCode()==b.hashCode() && a.equals(b) anyway. All you're doing is
moving where the delay happens.

It *really* gets on my nerves when developers do things like a ==
bufferedReader.readLine().intern() claiming that it speeds things up,
when the cost is just moving place. And in these situations, it's
their fault if they get out of OOM messages :-)

And with the point regarding 'fast lookup of messages' -- the Eclipse
messaging mechanism (by loading them into static variables) has
*exactly* the same effect, whilst not polluting any global pools.
Remember that pointer comparison will work when it's the same pointer,
and in the case it's not, it will do the equals() comparison. Unless
you're providing a really, really, really fast logging system then the
cost of printing/looking up a message is an unbelivably minor part of
a system. You probably have much more wastage in (e.g.)
StringBuffer().append(msg1).append(object).append(msg2), instead of
writer.write(msg1),write(object),write(msg2). For that matter,
parameterised messages (e.g. File {0} is broken) are going to cause
exactly these kind of StringBuffer-related horribleness.

The only time intern() should be used is by the compiler when
interning string literals in compiled source code. I'd go as far as
saying that the use of intern() should be marked as a warning, if not
an error, in any decent IDE.

Alex.

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to