On Fri, Feb 26, 2010 at 6:19 PM, Ulf Zibis <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 . -Dima