我们真的需要 NULL 吗?
Possible Duplicate:
What is the purpose of null?
Is there an actual need for NULL or not? In most of the OO languages i have programmed in there has always been a way to set a variable to a NULL value which lead to all sorts of funny problems.
What are your thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NULL 有点像上帝。 如果它不存在,我们最终将不得不创建一个。 某些东西必须代表未分配的引用的值(无论是因为它从未被分配还是在某个时刻被清除)。 唯一的选择是使用一个可以有效替代 NULL 的对象。 问题是,如果您做了所有这些都是为了避免 NullPointerException,那么现在您只需将其替换为 UnexpectedObject 异常或 ClassCastException 或其他异常。
NULL is a little like God. If it didn't exist, we would wind up having to create one. Something has to represent the value of a reference that is unassigned (whether that be because it was never assigned or it was cleared at some point). The only alternative is to use an object that, effectively, substitutes for NULL. The problem with that is that if you did all that to avoid the NullPointerException, now you're going to simply replace it with UnexpectedObject exception or ClassCastException or what not.
在具有垃圾回收功能的语言中,变量是实际存储位置(与 Python 的标签相反),需要 NULL 值以允许在变量作用域结束之前以干净的方式释放内存。
此外,甚至许多用伪代码编写的算法也使用特殊的
NULL
值。 它几乎无处不在。 它是计算机科学的核心概念。In languages with garbage collection where variables are actual storage locations (as opposed to Python's labels), the
NULL
value is required to allow memory to be freed in a clean manner before the end of the variable's scope.Also, even many algorithms written in pseudo code make use of the special
NULL
value. It pops up literally everywhere. It is a central concept in computer science.可以设计一种没有 NULL 的语言,而是将未初始化的值指向一个实际上不执行任何操作的单例虚拟对象。 您可以将指针与该虚拟对象的引用进行比较,并且调用该对象上的方法将导致不执行任何操作或出现运行时错误。
对于 C++ 或 Java 等静态类型语言来说,这项技术很难实现。
It's possible to design a language that doesn't have a NULL but instead uninitialised values point to a singleton dummy object that doesn't actually do anything. You could compare pointers against the reference of this dummy object, and calls to methods on the object would result in no action or a runtime error.
This technique is hard to implement for statically typed languages like C++ or Java.
通常有两个特殊值。 有些语言同时处理这两种情况,有些只处理 1 并与另一种一起抛出错误,有些则将两者合并。 这两个值是
Null
和Undefined
。Undefined
将尝试使用一个不存在的变量。Null
将尝试使用存在但没有值的变量。Null
可能很有用,因为它是一个保证值,表明有问题,或者超出了可能答案的域/范围。 以 Java 为例:如果您没有
null
,那么如果您在HashMap
中查找 Map 中不存在的内容会怎样? 你会返回什么? 如果您返回一个对象,那么您如何知道该对象是否意味着那里什么都没有,或者这就是地图中实际存在的内容。 解决方法可能包括创建您自己的“NON_EXIST”常量对象,但这本质上与null
已经存在的内容相同。 另一种解决方法可能是抛出异常。 现在您正在研究主要的性能影响。同样,它的想法是拥有一个可以使用的保证允许值。 它始终可供您使用,并且始终与通常从您正在执行的操作返回的“实际值”集脱节。 因此,
Null
被有意用来表示一些特殊的东西。就像我之前暗示的那样,这里也可以进行一些优化,因为
Null
是一个特殊值。 在像 Java 这样的语言中,如果您最初仅通过变量引用对象,然后将该变量设置为null
,您将删除该引用并允许 Java 的垃圾收集器收集现在未引用的数据。 相反,如果您让该变量永远存在,那块可能永远不会再次使用的内存将继续保存资源。 这是一个人为的示例,但它证明了一点,在一些资源密集型 Java 程序中,您将看到这些对null
的显式赋值。There are normally two special values. Some languages handle both, some only 1 and throw an error with the other, and some merge the two. Those two values are
Null
andUndefined
.Undefined
would be trying to use a variable that flat out doesn't exist.Null
would be trying to use a variable that exists but has no value.Null
can be useful because it is a guaranteed value that indicates that something is wrong, or outside of the domain/range of possible answers. Take Java for instance:If you did not have
null
, what if you did a lookup in aHashMap
for something that didn't exist in the Map? What would you return? If you returned an Object then how would you know wether that Object meant nothing was there or this was what was actually in the Map. Workarounds could include creating your own "NON_EXIST" Constant Object, but thats essentially the same thing as whatnull
already is anyways. Another workaround might be throwing an Exception. Now you're looking at major performance impacts.Again, its the idea of having a guaranteed allowable value that you can use. It is always available to you and its always disjoint from the set of "real values" that would normally return from an operation you're performing.
Null
is therefore intentionally used to mean something special.Like I hinted before, there are also optimizations that can be done here as well because
Null
is a special value. In languages like Java if you originally referenced an Object solely by a variable, then set that variable tonull
you're removing that reference and allowing Java's Garbage Collector to collect that now unreferenced data. If instead you let that variable sit around forever that hunk of memory that may never be used again will continue to hold resources. This is a contrived example, but it proves a point and in some resource intensive Java programs you will see these explicit assignments tonull
.