[jdom-interest] Reference comparisons in SAXHandler.startElem
	ent
    GB/DEV - Philip Nelson 
    philip.nelson at omniresources.com
       
    Tue Mar 13 18:26:23 PST 2001
    
    
  
> I believe equals() also short circuits if the lengths are unequal: to
> what extent does the test data compare different lengths (and does it
> make much difference?).
Yes it does make that check first but it really doesn't make much
difference.  Even stranger is the fact the "xyyyy".equals("yyyyy") is the
slowest in my samples.
I can't trace to the lines in the equals method but perhaps the thing that
takes the time is the cast.  It looks like if the strings are interned, it
won't make any difference whether we use == or .equals
from String.java
	public boolean equals(Object anObject) {
	if (this == anObject) {
	    return true;
	}
	if ((anObject != null) && (anObject instanceof String)) {
	    String anotherString = (String)anObject;
	    int n = count;
	    if (n == anotherString.count) {
		char v1[] = value;
		char v2[] = anotherString.value;
		int i = offset;
		int j = anotherString.offset;
		while (n-- != 0) {
		    if (v1[i++] != v2[j++]) {
			return false;
		    }
		}
		return true;
	    }
	}
	return false;
	}
    
    
More information about the jdom-interest
mailing list