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

(str) Error in description of the method indexOf in the class StringBuffer

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 8
    • 6, 8
    • core-libs
    • b106
    • Verified

        A DESCRIPTION OF THE PROBLEM :
        The bug concerns the description of the method
        public int indexOf(String str, int fromIndex) in the class StringBuffer.

        In the documentaion we can see following misleading lines:
        ===
        The integer returned is the smallest value k for which:
             k >= Math.min(fromIndex, str.length()) &&
                           this.toString().startsWith(str, k)
        If no such value of k exists, then -1 is returned.
        ===
        This statement is wrong. For example, consider this code:

                        StringBuffer sb = new StringBuffer( " deabcabab " );
        String str = " ab " ;
        int fromIndex = 6;
        int k = 2;
        System.out.println(k >= Math.min(fromIndex , str.length()));
        System.out.println(sb.toString().startsWith(str,k));
        System.out.println(k < sb.indexOf(str, fromIndex));
        System.out.println(sb.indexOf(str, fromIndex));
        output:
        ===
        true
        true
        true
        7
        ===




        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        ===
        The integer returned is the smallest value k for which:
             k >= Math.min(fromIndex, this.length()) &&
                           this.toString().startsWith(str, k)
        ===

        cf. with the description of indexOf for String.

        ACTUAL -
        ===
        The integer returned is the smallest value k for which:
             k >= Math.min(fromIndex, str.length()) &&
                           this.toString().startsWith(str, k)
        If no such value of k exists, then -1 is returned.
        ===

        URL OF FAULTY DOCUMENTATION :
        http://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html

              igerasim Ivan Gerasimov
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: