JDK-8016018 : (str) Error in description of the method indexOf in the class StringBuffer
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6,8
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2013-04-04
  • Updated: 2014-11-17
  • Resolved: 2013-08-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8
7u60Fixed 8 b106Fixed
Description
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