Java - the difference between empty string "" and null

empty string ""

①, Type: "" is an empty string (String) , the length is 0 , occupies memory , allocates a space in the memory , you can use the methods in the Object object . (Example: "".toString(), etc.)

②, memory allocation: means to declare a reference of string type, its value is "" empty string, this reference points to the memory space of str1.

null

①, Type: null is a null reference , indicating the value of an object , no memory is allocated , and calling the method of a null string will throw a null pointer exception . (eg: str1.endsWith(str2); java.lang.NullPointerException)

, memory allocation: means declaring a reference to a string object, but it points to null, that is to say, it does not point to any memory space.

Example:  String str1 = ""; //str1 corresponds to an empty string, declaring a reference to the object
String str2 = null; //str2 reference is empty

String str3 = new String(); //str3 will point to a specific String instance, the default value is ""

Note: str1 and str3 are instantiated, but str2 is not instantiated, but the addresses pointed to by str1 and str3 are different, but the values ​​are the same, both are empty.


null is not an object (null reference), "" is an object, so the comparison should be if(str1.equals("")) and if(str2 == null), that is, objects are compared with equals(), null is used, etc. number comparison. The correct way to write it is to first judge whether it is an object, and if so, then judge whether it is an empty string.


Additional instructions:

1. Objects of classes in Java are accessed by handles, similar to pointers in C.

2. There are two methods for judging equality in Java: "==" and equals() method. The former is compared according to the address. Only when the address and the value are equal, the two variables (reference types) are equal; the latter compares is the value of the variable, as long as the values ​​are equal, the two are equal.

3. In Java, variables and reference variables are stored in the stack (stack), and objects (new) 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.



Supplement 2:

Object o; //This way of writing just assigns a reference.
Object o = null; //Writing this way points to an empty object for the reference. The specific differences are as follows:
class Test {
    public static void main(String[] args) {
        Object o1;
        o1.toString(); /*The compilation fails here. The compiler only considers that o1 is a reference and does not point to any object, so the method cannot be called. */
        Object o2 = null;
        o2.toString(); /* Compilation can be passed here, but there is a null pointer exception. The compiler determines that o2 is an object, although it is a null object. */
    }
}

One is to name a non-existent person, and the other is to name a dead person. It's a fallacy that you tell non-existent people to eat, and it's a lie to tell dead people to eat. 
A null object is a special object that can be of any type. He is only there as a mark, and only exists so that the mark does not exist. There is no need to investigate what he is like in memory. null is just a token. A container can accept a null object, but a null reference is not.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325709968&siteId=291194637