Literally, NULL means null (determined with str==null), "" denotes an empty string (with Str.equals ("") or Str.length () ==0)
Question one:
The difference between null and ""
String S=null;
String.Trim () will be thrown as NullPointerException
String s= "";
String.Trim () will not throw, why?
For:
A null representation declares an empty object and is not a string at all.
"" represents an instance of an object that is an empty string of length 0.
Null represents an empty object that does nothing for an empty object except = and = =
"" is a string, but there is no content in this string.
String S=null; just defines a handle, which means you have a reference, but this reference does not point to any memory space
String S= ""; This reference has already pointed to a piece of memory space that is an empty string, is an actual thing, so you can manipulate it without worrying about it.
This is the same as the number 0 and no numbers.
Moreover, NULL can be assigned to any object, "" is not.
Here "" and Null are definitely two concepts
"" represents a string that exists, and its value is ""
Null means that the string has no actual value at all, and you do not know what it is.
It means string string = null and string string;
Null is an empty object, "" is an empty string
String S=null;//null is unallocated heap memory space
String a;//allocates a memory space and does not deposit any objects
String A= "";//allocate a memory space, save a string object
Question 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, and the third will output a.
What is this for? What do you do with these three statements?
For:
The first one simply defines a string variable s, and does not give it an initial value, in Java, the default is to give it an initial value (to reduce the risk) when using a variable.
The second and third define the string type variable s and give it an initial value, except 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 ' a ' in the string pool, and then use S to point to it.
Other than that
String s= "A" and string S=new string ("a"); there is a fundamental difference.
The former is to write a character ' a ' in the string pool, and then use S to point to it;
The latter is a string object that creates a "a" content on the heap.
String str= "AAA"; allocating memory on the stack
String Str=new string ("AAA"); allocating memory on the heap
String s; The system will automatically assign a value of NULL
String s; Just allocating a memory space to s
String s=null; The value stored in the allocated space is a null value
String s= "a"; I don't have to say that. The value of the allocated space is the character a
Question three:
Declares a string A; a variable
What is the difference between a== "" and a==null in future judgments?
For:
If you do not assign a value to a, a== "" causes an exception.
In practice, it is often assumed that "" and null represent the same meaning that all represent no value.
In this case, the following syntax is recommended:
if (A==null | | a.equals (""))
{
}
If A is null, no subsequent judgment is 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 don't assign a value to a, a== "" will cause an exception
So if (A==null | | a.equals ("")) {} This is the correct notation
Question four:
String Abc=null; String abc= ""; String ABC; What's the difference between the three types of notation?
For:
1: Create an empty string object,
2: Creates a string object that is empty.
3: Declares a String object, but does not allocate memory, and the memory is already allocated
For the last expression, you cannot have if (abc==null), or int length = Abc.length (), and the compilation will indicate that it may not be initialized.
String Abc=null;
String abc= "";
It is generally recommended to use the second type
The first kind of ABC points to NULL, many times to determine whether the string is empty, it is easy to miss this situation, when invoking the related method of string error
The second is relatively simple, the method of string can be used, the judgment of the time will not be wrong
1) String Abc=null;
2) String ABC;
3) String a= "";
4) String b= "";
5) string C=new string ("");
6) String D=new string ("");
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 when 3) 4) method than 5) 6) method left memory, the reason for doing so is
Because string is an immutable amount of content, the design pattern is used Gof.flyweight
But there is a key point, no one said, this is:
string s; Under what circumstances can be equated to string s=null; and under what circumstances is not equal?!
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, and the null will be printed.
The lines marked with an * number are automatically initialized (s is automatically initialized to null).
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.
This concludes:
In the definition of a member variable, string s; is equivalent to String s=null;
In the definition of a local variable (method variable), string s; is not equivalent to string s=null, at which point the S must be explicitly assigned.
These are small knowledge points, but in practical application is very important, it is easy to be ignored by some people, hereby proposed.
Another point to note is:
The main () method is no exception, and the compiler automatically assigns an initial value outside the method, as long as the variable is defined in the method.
The difference between null and ""