Java thousands ask _06 data structure (008) _null of which type

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What type of data does null belong to?

First, in Java, NULL itself is not an object, nor is it an instance of Objcet. What type of data does he belong to?
Understanding data types See here: What is the data type in Java
Java basic data types see here: [Java has 8 basic data types 3
See the following example:

if (null instanceof java.lang.Object) {    System.out.println("null属于java.lang.Object类型");} else {    System.out.println("null不属于java.lang.Object类型");}

Results:
Null is not part of the Java.lang.Object type

Conclusion: The null itself can represent an indeterminate object. But for null itself, it is not an object, nor is it an instance of Java.lang.Object.
Null is a special type, but you cannot declare a variable to be a null type, and the only value for a null type is NULL.

2. How to use NULL

In Java, NULL is a keyword used to identify an indeterminate object. Therefore, you can assign null to a reference-type variable, but you cannot assign null to a primitive type variable.
Like what:

intnull;//错误null;//正确

Null can be assigned to any class type or to any class type. In practice, NULL is generally used as a literal value, and the literal value can be any reference type.
Understanding Java literals See here: What is the literal value of the Java data type
In Java, variables follow a principle that is defined and then initialized before they can be used. We cannot assign a value to a after int a, to get the value of a. This is also true for reference variables.
For example:

        null;        try {            conn = DriverManager.getConnection("url""user""password");        catch (SQLException e) {            e.printStackTrace();        }        String catalog = conn.getCatalog();

If you do not specify conn = null at the beginning, a compilation error will occur.

Java thousands ask _06 data structure (008) _null of which type

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.