I was looking at the source for `java.lang.String` and noticed the [`equals` method](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/String.java#String.equals%28java.lang.Object%29) doesn't check whether the `char[]` backing each String is the same object. Wouldn't this improve compare times?

Supposed improvement contained in this rewritten version:

```
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) {
               char v1[] = value;
               char v2[] = anotherString.value;
               int i = offset;
               int j = anotherString.offset;
               /** Begin Optimization **/
               if(v1==v2 && i==j){
                   return true;
               }
               /** End Optimization **/
               while (n-- != 0) {
                   if (v1[i++] != v2[j++])
                       return false;
               }
               return true;
           }
       }
       return false;
   }

```

I believe this would improve performance in the case that the two Strings were obtained using `String.substring`, and possibly even interned Strings.

Does anybody know if there's a reason they chose not to implement it this way?

**Update:** For anybody who might not know a lot about the [implementation of String](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/String.java), there are cases other than the String pool where two String objects can have the same char[] value, int offset, and int count.

Consider the following code:

```
String x = "I am a String, yo!";
String y = x.split(" ")[3];
String z = x.substring(7,14);

```

You would end up with a situation like this:
![Debugger Expressions](https://i.stack.imgur.com/Tc4c0.png)

Also apparently the value-sharing feature of Strings has been done away with in Java 7u6 in order to satisfy some benchmarks. So if you spent time making your code run in decent time (or at all) by using String.substring() rather than String concatenation, you're SOL.