In Java, even the great gods don’t know null

First look at an example, readers can read and judge the output results by themselves:

public class TestClass {
    private static void testMethod(){
         System.out.println("testMethod");
    }
    public static void main(String[] args) {
         ((TestClass)null).testMethod();
    }
 }

The above example will output correctly: testMethod

Analysis: The first thing to understand is that this is for the call to the method by the class, not the call to the method by the object;

Secondly, the testMethod method is a static method, so you can use the "class method" directly, because the static method does not depend on whether the object is created. Null can be coerced into any type (not any type of object), so you can use it to execute static methods;

Finally, non-static methods are called in the way of "object. method", which can only be used after the dependent object is created. If the static before the testmethod() method is removed, a null pointer exception will be reported. The above point is also verified here. Of course, whether it is a static method or not, it already exists, but the access method is different 

 

Deep understanding

1. Null represents an uncertain object

    In Java, null is a keyword used to identify an uncertain object. Therefore, null can be assigned to reference type variables, but null can not be assigned to basic type variables. For example: int a=null; is wrong; and Ojbect o=null is correct. In Java, the application of variables follows a principle, defined first, and initialized before they can be used. We can't print the value of a without assigning a value to a after int a. This also applies to reference type variables.

    Sometimes, when we define a reference type variable, at the beginning, we can't give a definite value, but if we don't specify the value, the program may initialize the value in the try statement block. At this time, we will report an error when we use variables below. Therefore, you can assign a null value to the variable first, and the problem is solved. E.g:

Connection conn=null;
 try {
     conn=Drivermanager get Connection("url", user","password"); 
 }catch(SQLException e){
     e printstacktrace();  
  }
 String catalog=conn. getcatalog();

If conn=null is not specified at the beginning, an error will be reported in the last sentence.

 

Two, null itself is not an object, nor is it an instance of Objet

  Although null itself can represent an uncertain object, as far as null itself is concerned, it is not an object, it does not know what type, nor is it an instance of java.lang.Objecte. A simple example can be made:

// Isnul an object? Does it belong to the Object type? 
if(null instanceof java.lang.Object) 
    Systen.Out. println("null belongs to java.lang. Object type"); 
else 
    System.Out. println("null does not Belongs to Java.lang.Object type");

The result will be output:

null is not of type Java.lang.Object

Three, Java assigns values ​​to variables by default

    When defining a variable, if the variable is not assigned a value after the definition, Java will automatically assign a value to the variable at runtime. The assignment principle is that the integer type int, byte, short, long are automatically assigned the value 0; float and double with a decimal point are automatically assigned the value 0.0, and the boolean is automatically assigned the value false; other variables of the reference type are automatically assigned the value of null. Look through debugging.

 

Four, container type and null

  List: allows repeated elements, you can add any number of nullO;

  Set: Repeat elements are not allowed, at most null can be added;

  Map: Up to one null can be added to the key of the Map, and there is no limit to the value field;

  Array: basic type array, if no initial value is given after definition, the value will be automatically given when java is running; reference type array, if no initial value is given, all element values ​​are null.

 

Five, other functions of null

1. Judge whether a reference type data is null, use == to judge.

2. Release the memory and let a non-null reference type variable point to null, so that this object will no longer be used by any object, waiting for the JVM garbage collection mechanism to recycle it.

Guess you like

Origin blog.csdn.net/a159357445566/article/details/108603490