When I am free, I do not like to visit P station, but prefer to visit Programcreek and other technical websites. So that night, in the dead of night, I found a theme that can not be ignored but focuses on the basics. For example: what the hell is NULL in Java? Topics like soul torture are well worth exploring in depth.

Null is a special presence in Java because it goes hand in hand with the well-known NullPointerException (NPE). NPE creator Tony Hoare admitted in 2009 that “Null References was a ridiculous design, as if I had lost a billion dollars on a bet”.

So why does Java persist with NULL instead of killing it? I think it’s because null does bring more benefits to Java (I’ve pointed out some of them below, if you can spot them).

Let’s start with the following statement.

String s = null;

Copy the code

First, let’s review what a variable is and what a value is. To take a case in point, a variable is like a cave in which people may or may not live. People are values. In Java, if a variable wants to store a value, it must first declare its type.

S is a String variable, there’s no doubt about that, right? Oh, of course, brother. Cut the crap. No one can doubt that.

Java has two types, basic and reference. It is no wonder that variables declared as primitive types store values and variables declared as reference types store references to objects.

As in the previous statement, String is a reference type with a value of NULL, which means that s stores nothing, like a cave where no one lives.

If String s = “Silent King two”, it can be represented by the following graph.

The representation of String S = null is different; it has no referencable object.

So what exactly is null in memory? What exactly is null in Java?

Anyway, NULL is not a valid object, so there is no space allocated for it in memory, no place for it. Null is simply a notation that indicates that the reference does not point to any object at this time. The Java Virtual machine specification does not specify a specific value for NULL.

This not only reminds me of a classic line in the Buddhist Sutra, I think you have also guessed, read it out loud! “Color is empty, empty is color.” The Chinese character “empty” matches the null exactly, in other words, “null is empty, empty is null.” Classic, classic.

A member variable of a class that is a reference type defaults to NULL, unlike a primitive type.

public class Null {

    private String name;

    private int age;



    public static void main(String[] args) {

        Null n = new Null();



        System.out.println(n.name);

        System.out.println(n.age);

    }

}

Copy the code

The output of this code is:

null

0

Copy the code

But if it is a local variable that refers to a type, the compiler will remind us to initialize it.

If a variable does not currently have a defined value to initialize, null is the best option, known as deferred initialization, until it is actually used and then assigned to the “it actually” value (the first benefit of NULL). Even more amazing, NULL can be forced to convert.

String s = (String) null;

Integer i = (Integer) null;



System.out.println(s);

System.out.println(i);

Copy the code

The program not only compiles, but runs without throwing the dreaded NPE. However, Java has a rule that null assignments to primitives will fail compilation.

After all, null can only be used as an initializer for a reference type, but not for a base type, because the base type has its own initializer, such as zero for int. Don’t put too much faith in artificial intelligence, though. I’m sure I can confuse the compiler.

Integer j = null;

int k = j;

System.out.println(k);

Copy the code

Assigning null to the wrapper variable of the primitive type and then assigning it to the primitive type does nothing for the compiler. However, the NPE will be pulled out and flogged as it runs.

Another interesting fact about NULL is that instanceof will return false if a reference type variable with a null value is used.

String s1 = null;

System.out.println(s1 instanceof String); // false

Copy the code

That is, if the value of a reference type variable is not null and the ClassCastException is not thrown when the instanceof operator is used to determine the type, then the result is true. Otherwise, instanceof will not know that s1 is a String even if it explicitly declares a type for a variable, such as String s1 = null.

Okay, let’s look at other uses of NULL, such as nonexistence of objects and termination conditions.

The following is the Javadoc for the System.console() method, which returns the unique object (if any) associated with the current Java virtual machine; If not, return NULL.

What would Java return if it didn’t keep NULL? You need to define at least one more keyword that has the same meaning as null.

Consider another example, the readLine() method from the java.io.BufferedReader class

public String readLine(a) throws IOException



Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

Copy the code

This method returns the read string line by line until the end of the stream. How to determine the end of the stream, return null. In this case, we can use null as a condition for reading the string.

String line;

while((line = reader.readLine()) ! =null) {

   System.out.println(line);

}

Copy the code

Of course, it is possible to return another keyword, such as -1, to indicate that readLine() is at the end of the stream, but this is not much different from returning NULL.

Finally, I would like to offer a famous quote from Russell: “Being is reasonable.” Null has already existed, and it has its own reason for existence, regardless of its merits or wrongs.


Well, readers, that’s all for this article. Can see here are the most excellent programmers, promotion pay is you 👍. Original is not easy, if you feel a bit useful, please do not be stingy with your hands like the power.

PS: Of course, there are more beautiful Java original articles, more, here is not easy to list one by one, interested can go to the public account [silent King ii] reply [NULL] to obtain.