In this case, the relationship between string str and string str=null, which is almost nothing in the code, but string str=null; A pointer to an empty reference.
While string str is only defined, string str=null is initialized more than string str.
But string str=null points to a null reference, there is no space in memory, no actual memory.
If a string method is called through STR, a null pointer exception is reported.
Creating a string in this way, by "", is saved to the string pool in the string class, where space is allocated in memory.
That is, string str=null is not instantiated, and string = "" is instantiated and "" can be considered an object.
We verify by small program:
String str1 = "";
String str2 = null;
System.out.println (Str1.length ());
System.out.println (Str2.length ());
You can see that the output is:
0
Exception in thread "main" java.lang.NullPointerException
At Com.mytest.StringTrap.main (stringtrap.java:15)
In summary, string str=null, although initialized, is not instantiated, and string = "" is already instantiated, the former does not exist in memory, and the latter is actually present.
String = "" and string = NULL difference