Java language, the difference between null and "" the

null null object is, no address can be assigned to any object; "" is an empty string, the address but the content inside is empty, can only be assigned to a string object.
Such as: 
String S; // allocate a memory space, without any object stored  
String s1 = null; // define an object instance s1, but the examples refer to any memory space
String s2 = ""; // definition of an object instance s2, pointing to an empty string, a memory space is allocated, a string object saved
s1 can not be used directly, the method can not be called class string, or a null pointer exception will be reported, for example: s1.equals ( "aa") certainly will complain if we do not know whether a string is null, and have to use it, make a non-empty judgment!

其中s第一个是没有初始化的引用;s1为空引用;s2是在字符串池里写入一个空串"",然后用s2指向它。
 

Guess you like

Origin www.cnblogs.com/bingyimeiling/p/11499093.html