empty string ""
1, type:"" is an empty string (String), the length of 0, accounting for memory , in memory allocated a space , you can use object The method in. (For example: "". toString (), etc.)
2. Memory allocation: Declares a reference to a string type whose value is the empty string, which points to the str1 memory space.
NULL
1, type: null is a null reference , representing the value of an object , there is no memory allocated , the method of calling null string will throw a null pointer exception . (For example: Str1.endswith (STR2); Java.lang.NullPointerException)
2. Memory allocation: Represents a reference to declaring a string object, but points to null, which means that no memory space is pointed.
Example: String str1 = ""; STR1 corresponds to an empty string that declares the object's reference
String str2 = null; STR2 reference is empty
String str3 = new string (); STR3 will point to a specific string instance with the default value ""
Note: str1 and STR3 are instantiated, and str2 are not instantiated, but str1 and STR3 refer to different addresses, but the same values are null.
NULL is not an object (a null reference), "" is an object, so the comparison should be if (Str1.equals ("")) and if (str2 = = null), or the object is compared with Equals (), and Null is compared with the equals sign. The correct writing is to judge whether it is an object, and if so, to judge whether it is an empty string.
Supplemental NOTE: 1, Java class objects are used to access the handle, similar to the pointer in C.
2, in Java, there are two methods for determining equality: "= =" and the Equals () method, which is based on the address, and only the address and value are equal, and the two variables (reference types) are equal, and the value of the variable is equal as long as the value is equal.
3, in Java, variables and reference variables are in the stack (stack), and objects (new out) are stored in the heap (heap).
For example: string str = new String ("abc"),//str is stored in stack, the value of ABC is stored in heap and points to its allocated memory space.