What is the difference between null
and the \"\"
(empty string)?
I have written some simple code:
String a = \"\";
String b
There is a pretty significant difference between the two. The empty string ""
is "the string that has no characters in it." It's an actual string that has a well-defined length. All of the standard string operations are well-defined on the empty string - you can convert it to lower case, look up the index of some character in it, etc. The null string null
is "no string at all." It doesn't have a length because it's not a string at all. Trying to apply any standard string operation to the null string will cause a NullPointerException
at runtime.