Difference between null and ""
Question 1:
Difference between null and ""
String S = NULL;
String. Trim () will throw an empty exception.
String S = "";
String. Trim () won't be thrown. Why?
A:
Null indicates that an empty object is declared and is not a string at all.
"" Indicates that an object instance is declared. The value of this object instance is a null string with a length of 0.
Null indicates that an empty object is declared and no operation can be performed on the null object except = and =
"" Is a string, but there is no content in the string.
String S = NULL; only defines a handle, that is, you have a reference, but this reference does not indicate any memory space.
String S = ""; this reference has already pointed to a memory space that is a Null String. It is an actual stuff, so you can operate on it without worrying about anything.
Do you mean that the number 0 is not a concept ?? The same principle
In addition, null can be assigned to any object. "" will not work.
Here, "" and null are two concepts.
"" Indicates that a string exists and its value is ""
Null indicates that the string has no actual value, and you do not know what it is...
Oh, I understand, it means that string = NULL is the same as string;
Null is a null object "" is a Null String
String S = NULL; // null indicates that no heap memory is allocated.
String a; // a memory space is allocated and no objects are saved.
String A = ""; // a memory space is allocated and a string object is saved.
Question 2:
What is the difference between string s; and string S = NULL; and string S = "?
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.
Why? What are their respective statements?
A:
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 ).
Both the second and third define the string type variable S and assign it the initial value, except that the second value is null.
The main thing to understand is string s; S is a reference ~~ It is not an object
First, there is no initialization reference;
The second is an empty reference;
The third is to write a character 'a' in the string pool and point it to it with S.
In addition,
String S = "a" and string S = newstring ("A"); are essentially different.
The former is to write a character 'a' in the string pool, and then point to it with S;
The latter creates a String object with the content "A" on the stack.
String STR = "AAA"; // allocate memory on the stack
String STR = new string ("AAA"); // allocate memory on the heap
String s; the system automatically assigns null values.
String s; only allocate a memory space to S
String S = NULL; the value stored in the allocated space is null.
String S = "A"; I don't need to say that the value of the allocated space is character.
Question 3:
Declares a string a; variable
In future judgment, what is the difference between a = "" And a = NULL?
A:
If a is not assigned a value, a = "" will cause an exception.
In actual processing, we often think that "" and null represent the same meaning, that is, they both represent null.
The following syntax is recommended:
If (A = NULL | A = "")
{
}
If expression A is null, the subsequent judgment is not executed and true is returned directly.
Null is used to determine whether a bucket is allocated for the reference type.
"" Is for strings;
The string type is actually a string pointer, that is, a reference type.
Therefore, if A is not assigned a value, a = "" will cause an exception.
Therefore, if (A = NULL | A = "") {} is also correct.
Question 4:
String abc = NULL; string abc = ""; string ABC; What are the differences between the three methods?
A:
1: Create an empty string object,
2: Create a Null String object.
3: declares a String object, but no memory is allocated, and 1 or 2 has been allocated memory.
For the last representation, you cannot use if (ABC = NULL) or Int length = ABC. Length (). During compilation, the system prompts that initialization may not be performed.
String abc = NULL;
String abc = "";
The second type is generally recommended.
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.
The second method is relatively simple. The string method can be used, and no errors will occur during the determination.
1) string abc = NULL;
2) string ABC;
3) string a = "";
4) string B = "";
5) string c = new string ("");
6) string d = new string ("");
// 1) equal to 2). Unlike 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.
// 3) and 4), the variables A and B will point to the same memory address ("" address)
// 5) and 6), the variables c and d do not point to the same address, but two "" content addresses, and different from a and B. In fact, 3) and 4) it is equivalent to newstring (""). intern ().
// 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,
If not, generate an instance and place it in the pool to point to that address. It can be seen that 3) 4) the method for multiple references to the same content is greater than 5) 6) The Method Memory is left, the reason for doing so is:
Because string is an unchangeable amount of content, it uses the design pattern gof. flyweight.
But there is a key point. No one said that this is:
String s; under what circumstances can be equivalent to string S = NULL; under what circumstances is not equivalent ?!
Consider the following code:
// Stringtest. Java
Public class stringtest {
Static string s ;//*
Public static void main (string [] ARGs ){
// String s ;//**
System. Out. println (s );
}
}
Compile and run the above Code to print null.
The row marked with "*" is automatically initialized (S is automatically initialized to null ).
If you uncomment a line marked with **, the Code cannot be compiled because this line defines local variables, and local variables are not automatically initialized.
The conclusion is as follows:
In the definition of member variables, string s; is equivalent to strings = NULL;
In the definition of local variables (method variables), string s; is not the same as string S = NULL; in this case, s must be explicitly assigned a value.
Although these are small knowledge points, they are very important in practical application and are easily ignored by some people.
Another note is:
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.