#### Details

#### Description

Name: mgC56079 Date: 12/02/97

> From ###@###.### Tue Dec 2 06:47 MSK 1997

> To: "'JCK-comments'" <###@###.###>

>

> In this code:

> String s1 = ""; // empty string

> String s2 = ""; // empty string

> int n1 = s1.indexOf( s2 );

> int n2 = s1.indexOf( s2, 0 );

>

> the JDK sets both the variables "n1" and "n2" to -1.

> That is, the JDK reports that the empty string cannot

> occur within the empty string.

>

> This result both defies logic and is **NOT** in accord

> with the language specification.

>

> Consider: The JDK and the spec agree that the empty

> string is a subset of any non-empty string. That is,

> "xyz".indexOf( "" )

> is always zero. And if we put that in terms of sets, it

> makes sense: "The empty set is the subset of every

> set."

>

> But the JDK does not follow through on the full and proper

> definition: "The empty set is the subset of every set,

> INCLUDING the empty set."

>

> And, although it is obscure and difficult to follow, the JDK

> spec agrees with this.

>

> Specifically, in 20.12.26, the spec reads (in part):

>

> [The method indexOf returns] the smallest value

> "k" such that

> this.startsWith(str, k ) && ( k >= fromIndex )

> is true.

>

> In either of the cases shown above, "fromIndex" is zero,

> so "k" must simply be greater to or equal to zero, so

> the task is to find

>

> ...the smallest non-negative value "k" such that

> this.startsWith( str, k )

> is true.

>

> Well, the smallest non-negative value is, of course, zero.

> So let's test to see what

> this.startsWith( str, 0 )

> means.

>

> We now go to section 20.12.21, which says (after ignoring

> the subcases that do not apply in this situation):

>

> [the method startsWith returns] true if and only if

> this.subString( toffset ).startsWith( prefix )

> [is true].

>

> But here the value of "toffset" is zero, and 20.12.31 that,

> effectively, says that

> substring( 0 )

> returns a String object that is identical to the original.

>

> So we are finally effectively reduced to finding out if

> this.startsWith( prefix )

> is true, given that "prefix" is the empty string.

>

> And section 20.12.20 says SPECFICALLY

> "Note that the result will be true is the argument

> is an empty string..."

>

> In other words,

>

> "".startsWith( "" ) is TRUE

> so "".substring( 0 ).startsWith("") is TRUE

> so "".startsWith( "", 0 ) is TRUE

> so "".startsWith( "", k ) is TRUE for k==0

> and 0 is the minimum value of k such that k is >= 0

> so "".indexOf( "", 0 ) is 0.

>

> ***************

>

> Note that our in-house tests found this discrepancy between the

> JDK and the spec. So far as I know, the JCK String tests did

> NOT find this.

>

> Our version of class java.lang.String follows the spec, not the JDK,

> in this matter.

======================================================================