What do you think is in sArray[i]
before you +=
the String line
?
It's null of course!
Try this to see:
String N = null;
String M = N + "something";
System.out.println(M);
Using the the Java String +=
operator compiles to using StringBuilder
underneath. But each element is run through String.valueOf()
first.
And String.valueOf(null)
returns "null"
as a String.
So it evaluates to:
String N = null;
String M = StringBuilder.append(N).append("something").toString();
System.out.println(M);
Which in turn gives you:
String M = StringBuilder.append(null).append("something").toString();
Which ultimately gives you:
String M = StringBuilder.append("null").append("something").toString();
PS. The assignment of an incorrect length to the array is a separate issue. The length of each String and the length of the array are two different concepts.