From 53588b94f36e6f34d063e78d9a1be7832975bb7c Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Mon, 19 Oct 2009 10:36:48 -0600 Subject: [PATCH] compare lengths first in String.equals[IgnoreCase] for efficiency --- classpath/java/lang/String.java | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index 2233e4500b..e480b47be9 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -231,11 +231,25 @@ public final class String } 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) { - return this == s || (s != null && compareToIgnoreCase(s) == 0); + public boolean equalsIgnoreCase(String o) { + 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) {