Java: Why aren't NullPointerExceptions called NullReferenceExceptions?

后端 未结 5 2223
孤城傲影
孤城傲影 2020-11-30 07:45

Was this an oversight? Or is it to do with the JVM?

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 07:56

    In Java they use the nomenclature REFERENCE for referring to dynamically created objects. In previous languages, it is named POINTER. Just as the naming of METHODS in object oriented languages has taken over from former FUNCTION's and PROCEDURE's in earlier (object oriented as well as not object oriented) languages. There is no particular better or worse in this naming or new standard, it is just another way of naming the phenomenon of being able to create former dynamic objects, hanging in the air (heap space) and being referred to by a fixed object reference (or dynamic reference also hanging in the air). The authors of the new standard (use of METHODS and REFERENCES) particularly mentioned that the new standards were implemented to make it conformant with the upcoming planning systems - such as UML and UP, where the use of the Object oriented terminology prescribes use of ATTRIBUTES, METHODS and REFERENCES, and not VARIABLES, FUNCTIONS and POINTERS.

    Now, you may think now, that it was merely a renaming, but that would be simplifying the description of the process and not be just to the long road which the languages have taken in the name of development. A method is different in nature from a procedure, in that it operates within a scope of a class, while the procedure in its nature operates on the global scope. Similarly an attribute is not just a new name for variable, as the attribute is a property of a class (and its instance - an object). Similarly the reference, which is the question behind this little writing here, is different in that it is typed, thus it does not have the typical property of pointers, being raw references to memory cells, thus again, a reference is a structured reference to memory based objects.

    Now, inside of Java, that is - in the stomach of Java, there is an arbitration layer between the underlying C-code and the Java code (the Virtual Machine). In that arbitration layer, an abstraction occurs in that the underlying (bare metal) implementation of the references, as done by the Virtual machine, protects the references from referring bare metal (memory cells without structure). The raw truth is therefore, that the NullPointerException is truly a Null Pointer Exception, and not just a Null Reference Exception. However, that truth may be completely irrelevant for the programmer in the Java environment, as he/she will not at any point be in contact with the bare metal JVM.

提交回复
热议问题