A deep understanding of the null "type" in java and a deep understanding of null

Source: Internet
Author: User

A deep understanding of the null "type" in java and a deep understanding of null

This article focuses on examples of null "type" in java. The details are as follows.

First, we will give a simple null-related question, which will lead to our discussion of null. Later, we will explain the "type" of null according to the official language manual.

Question: Can the following programs run correctly?

Resolution:

Output should be: haha

Because null can be strongly converted to any class type, the previous (NULL) null) is valid, but after being forced to convert null, it is an invalid object and the return value is null, (This will be explained later)

The haha method is a static method, and the static method uses static binding. It does not throw a null pointer exception.

If the haha () function is changed to non-static, a null pointer exception is thrown.

Another example:

This question is similar to the above.

The result still outputs "haha"

In Java Language Specification, we can see that

Mentioned in section 4.1. The Kinds of Types and Values:

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 aspecial 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.

For Translation:

Java has two types:Basic TypeAnd the other isReference Type.

There is also a special null type, that is, the expression null type, which has no name.

Because the null type has no name, it is impossible to declare a variable of the null type or convert it to the null type.

Null reference is the only possible value of a null expression.

A null reference can be converted to any reference type.

In fact, the programmer can ignore the null type and think that null is only a special symbol that can become any reference type.

After reading this section, I was quite enlightened.

In section 5.2. Assignment Conversion:

"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 null type (null) reference is a unique value of this type) can be assigned to any type, A null reference (in fact, null) of this type of object is returned ).

The section 5.3. Method Invocation Conversion contains:

"A value of the null type (the null reference is the only such value) may be converted to any reference type ."

That is,"Null can be converted to any reference type.."

The official language manual provides a deep understanding of the null type.

It will be helpful for programming and job test interviews in the future.

Finally, I hope you will encounter some strange problems. Apart from Baidu, go to the Stack Overflow website as much as possible to search for them and check the official manual.

Summary

The above is all about a deep understanding of the null "type" in java. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.