classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: String.equals optimisation


From: Archie Cobbs
Subject: Re: String.equals optimisation
Date: Mon, 11 Jul 2005 20:27:54 -0500
User-agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.3) Gecko/20041129

Simon Kitching wrote:.
* Class.getName returns strings that have been interned. I don't
  think this is explicitly required by the java specs but is
  certainly true for Sun's JVM and seems likely to be done by
  any sensible JVM.

You definitely make some good arguments, but this one is not
neccesarily true. In fact, I'd argue a JVM that interns every
class' name (even if only on demand) is potentially wasting
a bunch of heap space.

I.e., is there something special about class names which means
they should be treated differently from any other String randomly
created and used in a Java application? (rhetorical question)
Otherwise, why not intern all Strings? Etc.

In any case, to provide two concrete counter-examples:

  $ cat > zz.java
  public class zz {
    public static void main(String[] args) {
        zz z = new zz();
        System.out.println(z.getClass().getName() == "zz");
    }
  }
  $ javac zz.java
  $ java zz
  true
  $ jc -Xint zz
  false
  $ jamvm zz
  false

On the other hand, comparing reference equality is very low cost,
so it seems like adding "==" to equals() might make good sense.

Of course, the "real" answer lies in empirical testing (something
I can't claim to have done).

-Archie

__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.com




reply via email to

[Prev in Thread] Current Thread [Next in Thread]