我们真的需要 NULL 吗?

发布于 2024-07-19 04:17:55 字数 259 浏览 8 评论 0原文

可能的重复:
null 的用途是什么?

是否确实需要 NULL ? 在我编写的大多数面向对象语言中,总是有一种将变量设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ゞ记忆︶ㄣ 2024-07-26 04:17:55

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.

ι不睡觉的鱼゛ 2024-07-26 04:17:55

在具有垃圾回收功能的语言中,变量是实际存储位置(与 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.

爱你不解释 2024-07-26 04:17:55

可以设计一种没有 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.

懒猫 2024-07-26 04:17:55

通常有两个特殊值。 有些语言同时处理这两种情况,有些只处理 1 并与另一种一起抛出错误,有些则将两者合并。 这两个值是 NullUndefined

  • 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 and Undefined.

  • 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 a HashMap 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 what null 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 to null 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 to null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文