Am 26.02.2010 16:27, schrieb Dmitry Nadezhin:


On Fri, Feb 26, 2010 at 6:19 PM, Ulf Zibis <ulf.zi...@gmx.de <mailto:ulf.zi...@gmx.de>> wrote:

    Am 26.02.2010 12:52, schrieb Dmitry Nadezhin:

        I found two alternatives in the link
        
http://mail.openjdk.java.net/pipermail/coin-dev/2009-December/002618.html
          The first alternative
        int equalByHashThreshold = 2;

        public boolean equals(Object anObject) {
          if (this == anObject) {
            return true;
          }
          if (anObject instanceof String) {

            String anotherString = (String)anObject;
            int n = count;
            if (n == anotherString.count&&
                (equalByHashThreshold == 0 || --equalByHashThreshold
        == 0)&&
                (anotherString.equalByHashThreshold == 0 ||
        --anotherString.equalByHashThreshold == 0)&&


                hash() == anotherString.hash()) {
        [snipped]
            }
          }
          return false;
        }

        will say that
        "A".equals("A") == false
        because body of if statement will not be executed


    I don't understand your problem.
    1. String literals are always interned, so "A" is always identical
    to "A". Therefore the body of the 1st if will return true.
    2. If the strings are not identical, body of 2nd if will be
    executed, if anObject is instance of String.


I'm sorry. The example should be
new String("A").equals("A") == false .
In this case Strings are not identical. The first if wil not return.
The second if will be executed.
The third if will not be executed because
(equalByHashThreshold == 0 || --equalByHashThreshhold == 0) <=> (2 == 0 || 1 == 0) <=> false .


Thanks for your patience.
As I think, adding a new member variable to String is a bad idea, even if it's transient, I didn't try to fix your claim.

But I've added a new equals4() method, which too follows the idea of incrementing a counter against a threshold, double-using the hash value, but without possibility to adjust the threshold against the string's length.

See new revision:
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/j7_EUC_TW/src/java/lang/String.java?rev=959&view=markup

-Ulf

Reply via email to