When Object variables are initially used in a language like Java, they have absolutely no value at all - not zero, but literally no value - that is null
For instance: String s;
If you were to use s
, it would actually have a value of null
, because it holds absolute nothing.
An empty string, however, is a value - it is a string of no characters.
String s; //Inits to null
String a =""; //A blank string
Null
is essentially 'nothing' - it's the default 'value' (to use the term loosely) that Java assigns to any Object variable that was not initialized.
Null
isn't really a value - and as such, doesn't have properties. So, calling anything that is meant to return a value - such as .length()
, will invariably return an error, because 'nothing' cannot have properties.
To go into more depth, by creating s1 = "";
you are initializing an object, which can have properties, and takes up relevant space in memory. By using s2;
you are designating that variable name to be a String, but are not actually assigning any value at that point.