Null and ""the Difference
Problem one: null and""the Difference
String s=NULLthe String.Trim () will throw an empty exception
String s=""; String.Trim () will not throw, why?
A: null means that an empty object is declared and is not a string at all. ""The delegate declares an instance of an object that is an empty string of length 0. A null representative declares an empty object and does nothing for an empty object, except= and = = ""It's a string, but there's no content in this string, string 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 a piece of memory space is an empty string, is an actual thing, so you can operate on it, without worrying about what you say the number 0 and is not a concept ah?? The same is true, and null can be assigned to any object,""It's not going to.""and Null are definitely two concepts""represents a string that exists, and its value is "" Null to represent that the string has no actual value at all and you do not know what it is ... Oh, I see, that means string string.=NULLstring string; Null is a null object""is an empty string s=NULL;//NULL is unallocated heap memory spaceString A;//allocated A memory space, no objects were depositedString a= "";//A memory space is allocated and a string object is savedquestion two: string S; and string S=NULL; and string s= "a"; What's the difference? 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. What is this for? What do you do with these three statements? A: The first only defines a string variable s, and does not give it an initial value, in Java, the default when using a variable must be given its initial value (reduce the risk). The second and third define the string type variable s and give it an initial value, except that the second one gives null (NULL). The main understanding is string s; S is a reference ~ ~ It is not an object the first is a reference with no initialization; the second is a null reference; The third is to write a character in the string poolA, then use S to point to it.
In addition, String s= "a" and string s=NewString ("a") is essentially the difference between writing a character in a string poolAand use S to point to it, and the latter is to create a content on the heap asAthe String object. String Str= "AAA";//allocating memory on the stackString str=NewString ("AAA");//allocating memory on the heapstring S; The system will automatically assign a value of nullstring s; just allocate a memory space to S. String S=NULLThe value that is stored in the allocated space is a null value of String s= "a"I don't have to say that anymore. The value of the allocated space is the character a problem three: a string A is declared, and a variable in a later judgment, a= = "" And A==null is different?Answer: If you do not assign a value to a, a==""can cause an exception. In actual processing, it is often considered that""and null represent the same meaning, which means no value. In this case, the following syntax is recommended:if(a==NULL|| a== ""{} If A is null, the subsequent judgment is not performed and returns true directly. Null is used to determine whether the reference type is allocated storage space""is for a string, the string type is actually a string pointer, which is also a reference type so if you do not assign a value to a, a==""can cause an exception so if (a==NULL|| a== ""{} This is also the correct question. Four: String ABC=NULL; String abc= ""; String ABC; What's the difference between the three types of notation? Answer:1: Creates an empty string object,2: Creates a string object with an empty string. 3: Declares a String object, but does not allocate memory, and the memory has been allocated for the last expression, you cannot if (ABC==NULL), or int length =abc.length (); When compiling, the hint may not be initialized. String ABC=NULL; String ABC=""; It is generally recommended to use the second first kind of ABC to point to null, many times to determine whether the string is empty, it is easy to miss this situation, in the call string of the relevant methods will be error The second is relatively simple, the method of string can be used, judge the time will not be wrong1) String abc=NULL; 2) String ABC; 3) String a= ""; 4) String b= ""; 5) String c=NewString (""); 6) String d=NewString (""); //1) is equal to 2), and 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//3) and 4), the variables A and B will point to the same memory address (the "" address)//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 string class maintains a string pool, for an assignment method such as 3 and 4), where string looks for strings that are already in the pool, and if so, points directly to that address.if not, generate an instance into the pool and point to that address, visible for the same content string multiple references 3)4) method than 5) 6), the only way to do this is because string is an immutable amount of content
The difference between "" and "null" in Java