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

(str) contentEquals checks the String contents twice on mismatch

    XMLWordPrintable

Details

    • b36

    Backports

      Description

        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.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                  Created:
                  Updated:
                  Resolved: