Null pointer problem and null

1. Null is a keyword in Java, like public, static, and final. It's case sensitive, you can't write null as Null or NULL, the compiler won't recognize them and throw an error.

2. Just like every primitive type has a default value, for example, the default value of int is 0, the default value of boolean is false, and null is the default value of any reference type, not strictly speaking, the default value of all object types. Just like you create a variable of type boolean that has false as its own default value, any reference variable in Java has null as its default value. This is true for all variables, such as member variables, local variables, instance variables, static variables (but the compiler will warn you when you use an uninitialized local variable). To prove this fact, you can observe the reference variable by creating a variable and then printing its value.

3. Null is neither an object nor a type, it is just a special value, you can assign it to any reference type, and you can convert null to any type.

4. Null can be assigned to reference variables, you cannot assign null to basic type variables, such as int, double, float, boolean. If you do that, the compiler will complain.

5. Any wrapper class with null value will throw a null pointer exception when Java unboxes the primitive data type. (eg when Integer is unboxed to int)

6. If a reference type variable with a null value is used, the instanceof operation will return false. (instanceof: used to indicate whether the object is an instance of a specific class at runtime, for example: Integer num = null, then calling num instanceof Integer will return false)

7. You cannot call a non-static method to use a reference type variable with a value of null, it will throw a null pointer exception; you can use a static method to use a reference type variable with a value of null, because static methods use static binding, No null pointer exception will be thrown.

8. You can use the == or != operations to compare null values, but you cannot use other arithmetic or logical operations such as less than or greater than. Unlike SQL, in Java null==null will return true.

Comment (what is the reference type? Here is an example: People is a class People duyitong;//duyitong is a reference type, where duyitong is null, after the next sentence is executed, it is similar to a pointer pointing to a People object in the heap
                                                                                                      duyitong=new People();                            )

Example

Suppose we now have a class, say String, for the following code:

String str = null;
if (str.equals("Hello World!")){
    System.out.println("Yes"); }else { System.out.println("No"); }

 

A null pointer exception will be thrown, but if we change the conditional judgment on the second line to:

if ("Hello World!".equals(str))

will not throw a null pointer exception, because String's equals method is not a Static method

reflection

In order to avoid memory overflow, we generally do not add the static keyword before methods that do not need to be called outside the class (because static methods will reside in memory, and their life cycle is consistent with the life cycle of the entire project), so we should When assigning the initial value, try not to use null to assign the initial value. If it must be null as the initial value, then when performing the operation, you must first Assert.isNull, try to avoid operating on null. Back to the equals method, if an object instance whose initial value may be empty, when calling the equals method, be sure to follow the "constant" .equals (variable) or the input .equals (before). In this way, you can avoid null pointer errors as much as possible, pay more attention and develop habits to prevent hidden dangers in the future.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325279955&siteId=291194637