The startsWith () method in Java returns true on an empty String. How?

Possible duplicate:
Why does "abcd" .StartsWith ("") return true?

The following simple Java code just uses the method startsWith().

package startwithdemo;

final public class Main
{    
    public static void main(String[] args)
    {
        System.out.println("My String".startsWith("M"));
        System.out.println("My String".startsWith("My"));
        System.out.println("My String".startsWith(""));
    }
}

It displays truein all cases. The first two cases are obvious, but in the latter case (with an empty string) it returns true. How?

+3
source share
4 answers

Since the API was developed, see javadoc .

, , . , , - . .


? ( )

A B , A B. A - , A ( ) B , B, . .

- . . {} - , - , {} {{ {} A {} {}.

. , set {} A. , {} A {} - , A. , {} A, {}. {} , , {} A.

+9

Javadoc:

: true, , , , ; . , true , * * String, equals (Object).

+3

It's true:

("" + "My String")obviously starts with "".

Same as the expression "My String"begins with""

+2
source
System.out.println(""+""+""+"string"=="string") // output is true;
+1
source

All Articles