Q:

String#split behavior when splitting the empty string according the "\s+"

I am a newbie in Java. Unfortunately, in Java there is a lot of stuff that is very hard to understand to a newbie.
For example,
String str = "";
String[] arr = str.split("\\s+");
System.out.println(Arrays.toString(arr));
System.out.println(arr.length);
System.exit(0);

The output is
[]
1

But why? I will appreciate if someone can explain me why the length of the array is 1.

A:

Even if the String instance is empty, it is still a String instance and the "nothing" must be put somewhere after split(). Thats the one element in the array.
If you printed str[0], you would get an empty string. The real "nothing" would be null but than you would get NullPointerException (you can't call split() on null value)

A:

Empty String is present at the Ist location of splitted array.
public static void main(String[] args) {
        String str = "";
        String[] arr = str.split("\\s+");
        System.out.println(Arrays.toString(arr));
        System.out.println(arr[0]);
        System.out.println(arr.length);
        System.exit(0);
    }

Output
[]

1

A:

Let's take a look at the implementation of Arrays.toString:
public static String toString(Object[] a) {
    if (a == null)
        return "null";
    int iMax = a.length - 1;
    if (iMax == -1)
        return "[]";

    StringBuilder b = new StringBuilder();
    b.append('[');
    for (int i = 0; ; i++) {
        b.append(String.valueOf(a[i]));  // Let's print that!
        if (i == iMax)
            return b.append(']').toString();
        b.append(", ");
    }
}

Now since the array is not empty, we'll get to the for loop.
We begin by appending [ to the result. Then we append String.valueOf(a[i]). Let's try to print it:
String str = "";
String[] arr = str.split("\\s+");
System.out.println(String.valueOf(arr[0]));

You'll see that the output is.. nothing! So the final result is:
[]

Please note that this has nothing to do with Java 8. The split method was introduced much earlier.

