What is null in Java?

后端 未结 14 1363
陌清茗
陌清茗 2020-11-21 20:49

What is null?

Is null an instance of anything?

What set does null belong to?

How is it represented in the me

相关标签:
14条回答
  • 2020-11-21 21:05

    Null in Java(tm)

    In C and C++, "NULL" is a constant defined in a header file, with a value like:

        0
    

    or:

        0L
    

    or:

        ((void*)0)
    

    depending on the compiler and memory model options. NULL is not, strictly speaking, part of C/C++ itself.

    In Java(tm), "null" is not a keyword, but a special literal of the null type. It can be cast to any reference type, but not to any primitive type such as int or boolean. The null literal doesn't necessarily have value zero. And it is impossible to cast to the null type or declare a variable of this type.

    0 讨论(0)
  • 2020-11-21 21:05

    null is a special value that is not an instance of any class. This is illustrated by the following program:

    public class X {
       void f(Object o)
       { 
          System.out.println(o instanceof String);   // Output is "false"
       }
       public static void main(String[] args) {
          new X().f(null);
       }
    }
    
    0 讨论(0)
  • 2020-11-21 21:12

    null in Java is like/similar to nullptr in C++.

    Program in C++:

    class Point
    {
        private:
           int x;
           int y;
        public:
           Point(int ix, int iy)
           {
               x = ix;
               y = iy;
           }
           void print() { std::cout << '(' << x << ',' << y << ')'; }
    };
    int main()
    {
        Point* p = new Point(3,5);
        if (p != nullptr)
        {
           p->print();
           p = nullptr;
        }
        else
        {
            std::cout << "p is null" << std::endl;
        }
        return 0;
    }
    

    Same program in Java:

    public class Point {
        private int x;
        private int y;
        public Point(int ix, int iy) {
            x = ix;
            y = iy;
        }
        public void print() { System.out.print("(" + x + "," + y + ")"); }
    }
    class Program
    {
        public static void main(String[] args) {
            Point p = new Point(3,5);
            if (p != null)
            {
                p.print();
                p = null;
            }
            else
            {
                System.out.println("p is null");
            }
        }
    }
    

    Now do you understand from the codes above what is null in Java? If no then I recommend you to learn pointers in C/C++ and then you will understand.

    Note that in C, unlike C++, nullptr is undefined, but NULL is used instead, which can also be used in C++ too, but in C++ nullptr is more preferable than just NULL, because the NULL in C is always related to pointers and that's it, so in C++ the suffix "ptr" was appended to end of the word, and also all letters are now lowercase, but this is less important.

    In Java every variable of type class non-primitive is always reference to object of that type or inherited and null is null class object reference, but not null pointer, because in Java there is no such a thing "pointer", but references to class objects are used instead, and null in Java is related to class object references, so you can also called it as "nullref" or "nullrefobj", but this is long, so just call it "null".

    In C++ you can use pointers and the nullptr value for optional members/variables, i.e. member/variable that has no value and if it has no value then it equals to nullptr, so how null in Java can be used for example.

    0 讨论(0)
  • 2020-11-21 21:13

    The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables.

    Also maybe have a look at

    null : Java Glossary

    0 讨论(0)
  • 2020-11-21 21:13

    An interesting way to see null in java in my opinion is to see it as something that DOES NOT denote an absence of information but simply as a literal value that can be assigned to a reference of any type. If you think about it if it denoted absence of information then for a1==a2 to be true doesn't make sense (in case they were both assigned a value of null) as they could really could be pointing to ANY object (we simply don't know what objects they should be pointing to)... By the way null == null returns true in java. If java e.g. would be like SQL:1999 then null==null would return unknown (a boolean value in SQL:1999 can take three values : true,false and unknown but in practise unknown is implemented as null in real systems)... http://en.wikipedia.org/wiki/SQL

    0 讨论(0)
  • 2020-11-21 21:14

    Bytecode representation

    Java's null has direct JVM support: three instructions are used to implement it:

    • aconst_null: e.g. to set a variable to null as in Object o = null;
    • ifnull and ifnonnull: e.g. to compare an object to null as in if (o == null)

    Chapter 6 "The Java Virtual Machine Instruction Set " then mentions the effects of null on other instructions: it throws a NullPointerException for many of them.

    2.4. "Reference Types and Values" also mentions null in generic terms:

    A reference value may also be the special null reference, a reference to no object, which will be denoted here by null. The null reference initially has no run-time type, but may be cast to any type. The default value of a reference type is null.

    0 讨论(0)
提交回复
热议问题