Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8060485

(str) contentEquals checks the String contents twice on mismatch

XMLWordPrintable

    • b36

        If you look at the String.contentEquals code, you will see:

            public boolean contentEquals(CharSequence cs) {
                ....
                // Argument is a String
                if (cs.equals(this))
                    return true;
                // Argument is a generic CharSequence
                char v1[] = value;
                int n = v1.length;
                if (n != cs.length()) {
                    return false;
                }
                for (int i = 0; i < n; i++) {
                    if (v1[i] != cs.charAt(i)) {
                        return false;
                    }
                }

        That is, if we pass a String in, and we mismatch, we do the per-char test as the fallback, which is guaranteed to "false" again.

              shade Aleksey Shipilev
              shade Aleksey Shipilev
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: