The research in this article mainly involves relevant examples of null "type" in Java, as detailed below.
First, give a simple null-related question, which will trigger our discussion of null. Later, we will interpret the null "type" according to the official language manual.
Question: Can the following program run correctly?
Analysis:
The output should be: haha
Because null can be forced to any class type, the previous ((NULL) null) is legal, but after null is forced to rotate, it is an invalid object, and its return value is null, (I will explain it later)
The haha method is a static method. The static method uses static binding and will not throw a null pointer exception.
If the haha() function is turned non-static, a null pointer exception will be thrown.
Let’s take another example:
This question is actually similar to the above.
The result is still outputting "haha"
We can see in Java Language Specification
In the section 4.1. The Kinds of Types and Values mentioned:
There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). ”
Type:
PrimitiveType
ReferenceType
There is also special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.
Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.
The null reference is the only possible value of an expression of null type.
The null reference can always undergo a widening reference conversion to any reference type.
In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.
I'll translate it for:
There are two types in the Java language, one is the basic type and the other is the reference type .
There is also a special null type, that is, the type of expression null, which has no name.
Because the null type has no name, it is impossible to declare a variable of null type or convert it to a null type.
null reference is the only possible value of a null type expression.
null references can be converted to any reference type.
In fact, programmers can ignore the null type and can think that null is just a special symbol that can become any reference type.
After reading this passage, I suddenly realized.
In the 5.2. Assignment Conversion section:
“A value of the null type (the null reference is the only such value) may be assigned to any reference type, resulting in a null reference of that type”
A value of a null type (null (null) reference is the only value of this type) can be assigned to any type and will return an empty reference to an object of that type (actually null).
In 5.3. Method Invocation Conversion section, there is:
“A value of the null type (the null reference is the only such value) may be converted to any reference type.”
i.e. " null can be converted to any reference type ."
Through the official language manual, I have a very deep understanding of the null type.
It is of some help to our programming and future job interviews.
Finally, I hope that everyone will encounter some strange problems. In addition to Baidu, try to search on the Stack Overflow website as much as possible and check the official manual.
Summarize
The above is all about this article's in-depth understanding of null "types" in Java, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!