1. NULL is neither an object nor a type, it is only a special value that you can assign to any reference type, or you can convert null to any type, for example:
Integer I=null;
Float F=null;
String S=null;
However, you cannot assign null to basic types, such as int, float,double, etc.
int k=null----------Compiler will error cannot convert from null to int
2.
null is a keyword, like public, static, final. It is case sensitive, you cannot write NULL as NULL or NULL, or the compiler will error
3. wrapper class with null value throws a null pointer exception when Java unboxing generates a basic data type
For example:Integer I=null;int k=i;---------------------------thrownjava.lang.NullPointerException
4. You need to add a null judgment when iterating through a collection or array, or it throws an exception when the collection or array contains null
5. When using equals to determine whether a string is equal, place the constant string on the left side of equals to prevent null pointer exceptions
For example:
String[] arr1={"abc", "123", NULL, "Sky"};
for (String s1:arr1) {
Boolean Flag=s1.equals ("Sky");
------------ When the value is =null, a null pointer exception is thrown and the S1.equals ("Sky") is changed to "Sky". Equals (S1) to avoid throwing exceptions
6. Empty string and Null difference
Type
Null represents the value of an object, not a string. For example, declaring a reference to an object, String a = null;
"" represents an empty string, meaning that it is 0 in length. For example, declare a string str = "";
Memory allocation
String str = NULL; Represents a reference that declares a string object, but points to null, which means that no memory space has been pointed to;
String str = ""; Represents a reference to a string type, with a value of "" an empty string, which refers to the memory space of an empty string;
In Java, variables and reference variables are present in the stack (stack), and objects (new) are stored in the heap (heap):
What is null in Java, and what to note in using