10-point explanation of NULL in Java

Source: Internet
Author: User

For every Java programmer, NULL is definitely a headache, and even the inventor of Java acknowledges that this is a huge design mistake, and today it's about NULL in Java.

1.null does not belong to any type, can be converted to any type, but with instanceof forever returns false.
2.null can never and eight basic data types of assignment operations, or not compile error, is to run an error.
3.null can be used to perform operations with strings.
4. The same type of NULL, the comparison returns True,null==null also returns True.

Continuous update ...

The following 10 examples will be used to verify the above four points

First, take a look at the following interview questions:

public class null{
public static void Nihao () {
System.out.println ("Hello Stranger");
}
public static void Main (string[] args) {
((null) NULL). Nihao ();
}
}
A, Hello, stranger.
B, compilation does not pass
C, compile pass, execute error
D, Memory overflow

The answer is a.

The following summary will give you a clear idea of why the above program has become such a result:

1.null is a keyword in Java that does not belong to any type, it is a special value, or it can be said to represent an indeterminate object.

2.null is the default value for all reference types.

3.null can be converted to any type, output null does not throw any exception, which is related to the source code of println ().

4.null can be converted to any type, so a static method can be called.

5.null can be converted to any type, but non-static methods cannot be called, and the runtime will have an error.

6.null is strongly converted to any type, it is only a special value and does not belong to any type.

7.null==null returns True, is converted to the same type of NULL, all return True, the different types of direct compilation error.

8. string can be performed by using a string converted to NULL, because the compiler specifically optimizes NULL when it is concatenated, in fact it is a StringBuilder, called append () method is a special handling of NULL when NULL is converted to "null" and the Last Call to ToString () returns a string object.

9. after the conversion of the eight basic types of NULL, the basic type of operation can not be performed, or a compile or run error occurs.

10.null and "" are not equal, because the following S does not point to a piece of memory, and S1 points to an empty string of memory.

10-point explanation of NULL in Java

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.