The difference between String str1 = "" and null in java

Scanner in = new Scanner (System.in); 
        String str2 = ""; // "" allocates space, so str1 is not an instantiated object, and str2 has been instantiated. null is not an object, "" is an object 
        System.out.println (str2.length ()); 
        String str1 = null ; // str1 reference is empty. No space allocated, 
        System.out.println (str1); 
        System.out. println (str1.length ());

 

Guess you like

Origin www.cnblogs.com/cocobear9/p/12683200.html