First Look at an example, the reader can first read the results of their own judgment:
1 Public classTestClass {2 Private Static voidTestMethod () {3System.out.println ("TestMethod");4 }5 Public Static voidMain (string[] args) {6((TestClass)NULL). TestMethod ();7 }8}
The above example will output correctly:
testMethod Analysis: The first thing to understand is that this is a call to the method on the class, not the object's call to the method;
second, the TestMethod method is to Statici a static method, directly using the "class method", because the static method uses no dependent on whether the object is created.
null can be coerced into any type (Not any type of object), so it can be used to execute static methods;
Finally, non-static methods are called with the "object. Method" method, which must depend on the object being created before it can be used, and if the static before the TestMethod () method is removed, the null pointer exception is reported. The above view is also verified here. Of course, whether it's static or not, it's already there, and it's just a different way of accessing it. ------------------------------------------------------------------------
Deep Understandingone, null represents an indeterminate objectin Java, NULL is a keyword used to identify an indeterminate object. You can therefore assign null to a reference type variable, but you cannot assign null to the base type slew amount. For example: int a=null; Is wrong, and Ojbect O=null is right. In Jin Ava, the applicability of variables follows a principle, defined first, and initialized before it can be used. We cannot assign a value to a after int a, and then print the value of a. This is also true for reference type variables. Sometimes, we define a reference type variable that, at the beginning, cannot give a definite value, but does not specify a value, and the program may initialize the value in a try statement block. At this point, we will use the variables below the error. Therefore, you can specify a null value for the variable before the problem is resolved. For example:
1 Connection conn=null; 2 Try { 3 4 }catch(SQLException e) {5 e printstacktrace ();
6}7 String catalog=conn. GetCatalog ();
If you do not specify Conn=null at the beginning, the last sentence will be an error. two, null itself is not an object, nor is it an instance of objet, although the null itself can represent an indeterminate object, but null itself, it is not an object, does not know what type, nor is it an instance of JAVA.LANG.OBJECTE. you can do a simple example:
// is nul an object ? if (nullinstanceof java.lang.Object) Systen.out. println ("null belongs to Java.lang. Object Type "); Else System.out. println ("NULL is not part of the Java.lang.Object type");
The result is output:
Null is not part of the Java.lang.Object type
Third, Java default to assign values to variables
when defining a variable, if the variable is not assigned a value after it is defined, Java will automatically assign a value to the variable at run time. The assignment principle is an
automatic assignment of integers of type int, Byte, short, and long to 0, a
float with a decimal point, a double automatically assigned avalue of 0.0, and a
Boolean auto-assigned value of False The other
reference type variables are automatically assigned null, which can be seen through debugging. iv. container type and null
List: Allow repeating elements, can join any multi-nullo;
Set: Duplicate elements are not allowed and can be added to null at most;
map:map Key can join ー a null,value field no limit;
array: An array of primitive types that, if not given an initial value, is automatically given by the Java runtime, and if an array of reference types is not given an initial value, all element values are null.
v. Other effects of NULL
1. Judging whether a reference type data is null, use = = to judge.
2. Free up memory, so that a non-null reference-type variable points to null, so that the object is no longer applied by any object, waiting for the JVM garbage collection mechanism to be recycled.
[Java Learning Basics] Analysis of Java method invocation