compare lengths first in String.equals[IgnoreCase] for efficiency

This commit is contained in:
Joel Dice 2009-10-19 10:36:48 -06:00
parent 1b893c7a19
commit 53588b94f3

View File

@ -231,11 +231,25 @@ public final class String
} }
public boolean equals(Object o) { public boolean equals(Object o) {
return this == o || (o instanceof String && compareTo((String) o) == 0); if (this == o) {
return true;
} else if (o instanceof String) {
String s = (String) o;
return s.length == length && compareTo(s) == 0;
} else {
return false;
}
} }
public boolean equalsIgnoreCase(String s) { public boolean equalsIgnoreCase(String o) {
return this == s || (s != null && compareToIgnoreCase(s) == 0); if (this == o) {
return true;
} else if (o instanceof String) {
String s = (String) o;
return s.length == length && compareToIgnoreCase(s) == 0;
} else {
return false;
}
} }
public int compareTo(String s) { public int compareTo(String s) {