JavaRush /Java Blog /Random EN /Is null an object in Java?

Is null an object in Java?

Published in the Random EN group
Is null an object? I can absolutely say that it is not. In the sense that (null instanceof Object)it will give the value false. Here's what else you need to know about null:
Is null an object in Java?  - 1
  1. You cannot call a method with the value null: x.m()it will throw an error when xthe value is null and mthe method is non-static. By the way, if mit is a static method, everything is fine, since only the class is important x, and the value is ignored.

  2. There is only one null for all, and not a separate one for each class. Thus, ((String) null == (Hashtable) null)for example.

  3. It is perfectly acceptable to pass null as a parameter to a method if the method allows it (some methods allow a null parameter, others do not). So, for example, you may well write System.out.println (null), but it butstring.compareTo (null)won’t work. So always indicate in the javadoc comments of your methods whether null is acceptable as their parameters, if it is not completely obvious.

  4. In the JDK from version 1.1 to 1.1.5, passing null as a literal argument to the constructor of an anonymous inner class (for example, new SomeClass(null) { ...}caused a compiler error. However, you can pass an expression with the value null, or pass a forced null, in the formnew SomeClass((String) null) { ...}

  5. There are at least three different cases, which are usually expressed using null:

    • Uninitialized (no initialization). A variable or slot that has not yet been assigned any value.

    • Non-existant/not applicable . For example, the terminal nodes of a binary tree can be called ordinary nodes with null children.

    • Empty (emptiness of something). For example, you can use null to represent an empty tree. Note that this is quite different from the previous case (they are often confused in practice). The difference is whether null is an acceptable tree node or whether null means the value does not need to be treated as a tree node.

    Compare three implementations of binary tree nodes with sequential console output methods:


  6. // null означает «не применимо»
    // Здесь нет пустого дерева.
    class Node {
      Object data;
      Node left, right;
    
      void print() {
        if (left != null)
          left.print();
        System.out.println(data);
        if (right != null)
          right.print();
      }
    }

  7. // null означает пустое дерево
    // обратите внимание на статические и нестатические методы
    
    class Node {
      Object data;
      Node left, right;
    
      void static print(Node node) {
        if (node != null) node.print();
      }
    
      void print() {
        print(left);
        System.out.println(data);
        print(right);
      }
    }

  8. // Отдельный класс для Empty
    // null не используется
    
    interface Node { void print(); }
    
    class DataNode implements Node{
      Object data;
      Node left, right;
    
      void print() {
        left.print();
        System.out.println(data);
        right.print();
      }
    }
    
    class EmptyNode implements Node {
      void print() { }
    }
Author of the answer: Peter Norvig, American scientist in the field of artificial intelligence, director of research at Google
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION