I encountered a problem at work today:
InCodeMedium:
1 //The name can be "", that is, name = ""2 //However, name cannot be null, that is, name! = NULL
The following is the difference between null and "" in Java:
1 Null and "" Difference 2 Question 1: 3 Null and "" Difference 4 String S = Null ; 5 String. Trim () will throw an empty exception. 6 String S = "" ; 7 String. Trim () won't be thrown. Why? 8 A: 9 Null indicates that an empty object is declared and is not a string at all. 10 "" Declares an object instance. The value of this object instance is a null string with a length of 0. 11 12 Null indicates that an empty object is declared and no operation can be performed on the null object except = and =13 "" It is a string, but there is no content in the string 14 15 String S = Null Only defines a handle, that is, you have a reference, but this reference does not point to any memory space. 16 String S = "" This reference has already pointed to a memory space that is a null string and is an actual stuff, so you can operate on it without worrying about anything. 17 18 Do you mean that the number 0 is not a concept ?? The same principle 19 20 In addition, null can be assigned to any object ."" It won't work. 21 22 Here "" And null are two concepts. 23 "" Indicates that a string exists and its value is "" 24 Null indicates that the string has no actual value, and you do not know what it is... 25 26 Oh, I understand, it means string = Null It is the same thing as string. 27 28 Null is an empty object "" Is a Null String 29 String S = Null ; // Null indicates that no heap memory is allocated. 30 String; // A memory space is allocated and no objects are saved. 31 String A = ""; // Allocates a memory space and stores a String object. 32 33 34 35 Question 2: 36 String s; and string S = Null ; And string S = "" ; What is the difference? 37 For the three cases, when out. println (s); is used, the first one has an exception, and the second one outputs null. The third one outputs. 38 Why? What are their respective statements? 39 40 A: 41 The first is to define a string-type variable s without assigning an initial value to it. In Java, the initial value must be assigned when a variable is used by default (to reduce risks ). 42 Both the second and third define the string type variable S and assign it the initial value, except that the second value is null. 43 44 The main thing to understand is string s; S is a reference ~~ It is not an object 45 First, there is no initialization reference; 46 The second is an empty reference; 47 The third is to write a character 'a' into the string pool' And then point to it with S. 48 In addition, 49 String S = "a" and string S = New String (""); There are essentially differences 50 The former is to write a character 'a' into the string pool' , And then point to it with S; 51 The latter creates a content on the stack as "" . 52 String STR = "AAA "; // Allocate memory on the stack 53 String STR = New String ("AAA "); // Allocate memory on the heap 54 55 String s; the system automatically assigns null values. 56 57 String s; only allocate a memory space to S 58 String S = Null ; Is the value stored in the allocated space is null 59 String S = "" In this statement, the value of the allocated space is character. 60 61 62 63 Question 3: 64 Declares a string a; variable 65 In future judgment, what is the difference between a = "" And a = NULL? 66 67 A: 68 If a is not assigned a value, a = "" Exceptions may occur. 69 In actual processing, we often think that "" And null represent the same meaning, that is, they all represent no value. 70 The following syntax is recommended: 71 If (A = Null | A = "") 72 { 73 } 74 If expression A is null, the subsequent judgment is not executed and true is returned directly. 75 76 Null is used to determine whether a bucket is allocated for the reference type. 77 "" Is for strings; 78 The string type is actually a string pointer, that is, a reference type. 79 Therefore, if A is not assigned a value, a = ""Will cause exceptions 80 So if (a = Null | A = "" ) {} Is correct. 81 82 83 Question 4: 84 String abc = Null ; String abc = "" ; String ABC; What are the differences between the three methods? 85 86 A: 87 1: Create an empty string object, 88 2 : Create a Null String object. 89 3 : Declares a String object, but does not allocate memory, and 1 or 2 has allocated memory. 90 For the last representation, you cannot Null ), Or Int length = ABC. Length (); during compilation, the system prompts that initialization may not be performed. 91 92 93 String abc = Null ; 94 String abc = "" ; 95 The second type is generally recommended. 96 The first type of ABC points to null. In many cases, it is easy to miss this case when determining whether the string is null or not. Errors will occur when the string-related method is called. 97 The second method is relatively simple. The string method can be used, and no errors will occur during the determination. 98 99 1) string abc = Null ; 100 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) equal to 2). Different from the C language, Java does not allow a hanging reference for security reasons. Any referenced address without a value is automatically assigned null to prevent access to any memory. 106 // 3) and 4), 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 addresses of two "" contents, and different from a and B. In fact, 3) and 4) it is equivalent to new string (""). intern (). 108 // The string class maintains a string pool. For a value assignment method such as 3) and 4), string searches for whether the string is already in the pool, direct to this address, 109 110 If not, generate an instance and place it in the pool to point to that address. It can be seen that the method for multiple references of strings with the same content is 3) 4) 6) ) Method, the reason for doing so is 111 112 Because string is an unchangeable amount of content, it uses the design pattern gof. flyweight. 113 114 115 116 117 But there is a key point. No one said that this is: 118 String s; under what conditions can be equivalent to string S = Null Under what circumstances is it not equivalent ?! 119 Consider the following code: 120 // Stringtest. Java 121 Public Class Stringtest { 122 Static String S; // * 123 Public Static Void Main (string [] ARGs ){ 124 // String S; // ** 125 System. Out. println (s ); 126 } 127 } 128 Compile and run the above Code to print null. 129 Visible labels * The row ID is automatically initialized (S is automatically initialized to null ). 130 However, if you mark ** The Code cannot be compiled because this line defines local variables, and local variables are not automatically initialized. 131 The conclusion is as follows: 132 In the definition of member variables, string s; is equivalent to string S = Null ; 133 In the definition of local variables (method variables), string S is not the same as string S = Null In this case, values must be explicitly assigned to values using S. 134 Although these are small knowledge points, they are very important in practical application and are easily ignored by some people. 135 Another note is: 136 As long as the initial values are displayed in the variables defined in the method, the main () method is no exception, and the compiler automatically assigns the initial values outside the method. 137 138