There is a problem at work today:
In the code:
1//name can be "", that is: name = = "" 2//But name is not NULL, that is: name! = NULL
The following are the differences between null and "" in Java:
1 The difference between null and "" 2 Problem One: 3 null and "" The difference between 4 String s=null; 5 String.Trim () will be thrown as an empty exception 6 string s= ""; 7 String.Trim () will not throw, why? 8 A: 9 null means that an empty object is declared and is not a string at all. 10 "" represents an instance of an object that is an empty string with a length of 0. Null represents an empty object, do nothing to empty objects, except = and = = 13 "" is a string, but this string is not the content of the strings s=null; just define a handle, which means you have a reference, but this reference Does not point to any memory space. String S= ""; This reference has pointed to an empty string of memory space, is a real thing, so you can operate on it, without worrying about what 17 18 you say the number 0 and not is a concept ah?? That's the same thing. 19 20 Moreover, NULL can be assigned to any object. "" Will not. 21 22 Here "" and Null are absolutely two concepts 23 "" means a string exists, and its value is "" "Null for the string there is no actual value, you do not know what it is ... 25 26 Oh, I get it. String string = null and string string; Null is a null object "" is an empty string, string S=null;//null is Unallocated heap memory space The string a;//allocated a memory space and did not deposit any objects to the string a= "";//allocate a memory space, save a string object 32 33 34 35 Question two: the string s; Tring s=null; and string s= "a"; what's the difference? 37 for these three cases, use OUT.PRINTLN (s), the first one will appear an exception, the second will output null. The third one will output a. 38 What is this for? What do you do with these three statements? 39 40 A: 41 The first one simply defines a string variable s, and does not assign an initial value to it, in Java, the default is to use a variable of theIt must be given an initial value (risk reduction). 42 the second and third define the string type variable s and give it an initial value, except that the second one gives null (NULL) 43 44 The main understanding is string s; s for a reference ~ ~ It is not an object 45 the first is a reference with no initialization; 46 is a null reference; 47 The third one is to write a character ' a ' in the string pool, and then use S to point to it. 48 In addition, the s= string "a" and string S=new string ("a"), there is a fundamental difference between 50 the former is to write a character ' a ' in the string pool, and then point to it with S; 51 The latter is to create a string object on the heap with content "a". str= String "AAA"; Allocates memory on the stack of the string Str=new string ("AAA"); Allocate memory on the heap, the String s; The system automatically assigns a value of NULL to the string s; just allocates a memory space to s; the value stored in the allocated space is the null value of the S=null string s= "a"; I don't have to say that. The value of the allocated space is a character a 60 61 62 63 Question three: 64 declares a string A; variable 65 what is the difference between a== "" and a==null in future judgments? 66 67 A: 68 If you do not assign a value to a, a== "" will cause an exception. 69 in actual processing, it is often assumed that "" and null represent the same meaning, that is, no value. 70 The following syntax is recommended at this time: A==null | | a== "") 72 {73} 74 If A is null, the subsequent judgment is not performed and returns true directly. The null is used to determine whether the reference type is allocated a storage space of 77 "" is for a string, the string type is actually a string pointer, that is, a reference type 79 so if you do not assign a value to a, a== "" will cause an exception 80 so if (a ==null | | a== "") {} This is also the correct notation 81 82 83 Question four: the String abc=null; String abc= ""; StriNG ABC; What's the difference between three types of notation? 85 86 A: 87 1: Create an empty string object, 88 2: Creates a string object with a null string. 89 3: Declares a String object, but does not allocate memory, and the memory has been allocated 90 for the last expression, you cannot if (abc==null), or int length = Abc.length (); The abc=null of the String; 94 String abc= ""; 951 recommended to use the second 96 of the first ABC point to NULL, many times to determine whether the string is empty, it is easy to miss this situation, when invoking the relevant method of string error 97 the second type is relatively simple, the method of string can be used, judge the time will not be error 98 1) String Abc=null; 2) String ABC; 101 3) String a= ""; 102 4) String b= ""; 103 5) String C=new string (""); 104 6) String D=new string (""); 105//1) is equal to 2), unlike the C language, Java for security reasons do not allow a hanging reference, no assignment of the reference address is automatically assigned to NULL to prevent access to any memory 106//3) and 4), the variables A and B will point to the same memory address ("" address) 107//5) and 6), the variables C and D do not point to the same address, but the address of the two "" content, and, unlike A, B, in fact, 3) and 4) are equivalent to the new String (""). Intern (). The 108//string class maintains a string pool, for example 3 and 4) Such an assignment method, string will find in this pool whether the string is already in the pool, if so, directly point to that address, 109 110 if not, generate an instance into the pool and point to that address, Visible for the same content string multiple references when 3) 4) method than 5) 6) method of remaining memory, the reason for doing so is 111 112 because string is an immutable amount of content, using the design pattern gof.flyweight 113 114 115 116 117 But there is a key point, no one said, that is: 118 String s;The case can be equated to string s=null, and under what circumstances is not equal?! 119 Consider the following code://stringtest.java 121 public class Stringtest {122 static String s; * 123 public static void Main (string[] args) {124//string s; * * SYSTEM.OUT.PRINTLN (s); 126} 127} 128 compile and run the above code to print null. 129 visible lines marked with an * number are automatically initialized (s is automatically initialized to null). 130 and if you uncomment a line marked with a * * number, the code cannot be compiled because the row defines a local variable, and the local variable is not automatically initialized. 131 It concludes that: 132 in the definition of a member variable, string s; is equivalent to string s=null;133 and in the definition of a local variable (method variable), string s; is not equivalent to string s=null; At this point, the S must be explicitly assigned. 134 Although these are small knowledge points, but in practical application is very important, it is easy to be ignored by some people, hereby proposed. 135 Another point to note is that: 136 only if the method in the definition of variables to display the initial value, the main () method is no exception, and outside the method of the compiler to automatically assign the initial value. 137 138