C++ 中的 NULL 有什么区别?和Java中的null?
我一直试图找出为什么 C++ 让我疯狂地输入 NULL
。前几天突然想到了;多年来我一直在 Java 中输入 null
(小写)。现在我突然开始用 C++ 编程,那一小块肌肉记忆让我发疯。
维基百科将 C++ NULL 定义为 stddef 的一部分:
扩展为空指针的宏 持续的。它可以定义为 ((void*)0)、0 或 0L 取决于 编译器和语言。
Sun 的文档告诉我关于 Java 的“空文字”:
null 类型只有一个值,即 null 引用,由文字表示 null,由 ASCII 构成 人物。空文字始终是 null 类型。
所以这一切都非常好。我知道什么是空指针引用,并且感谢您的编译器注释。现在我对 Java 中文字的概念有点模糊,所以我继续阅读......
文字是源代码 固定值的表示; 文字直接表示为 你的代码不需要 计算。
还有一个特殊的空文字 可以用作任何值 参考类型。可以指定 null 任何变量,除了变量 原始类型。很少有你 可以使用超出的空值 测试其存在。所以, null 经常在程序中用作 标记来指示某个对象是 不可用。
好的,我想我现在明白了。在 C++ 中,NULL 是一个宏,编译时计算结果为空指针常量。在Java中,null是一个固定值,任何非基元都可以被赋值;非常适合在方便的 if 语句
中进行测试。
Java 没有指针,所以我明白为什么他们将 null 保留为一个简单的值而不是任何花哨的值。 但是为什么java决定将全部大写NULL
更改为null
?
此外,我在这里遗漏了什么吗?
I've been trying to figure out why C++ is making me crazy typing NULL
. Suddenly it hits me the other day; I've been typing null
(lower case) in Java for years. Now suddenly I'm programming in C++ and that little chunk of muscle memory is making me crazy.
Wikiperipatetic defines C++ NULL as part of the stddef:
A macro that expands to a null pointer
constant. It may be defined as
((void*)0), 0 or 0L depending on the
compiler and the language.
Sun's docs tells me this about Java's "null literal":
The null type has one value, the null
reference, represented by the literal
null, which is formed from ASCII
characters. A null literal is always
of the null type.
So this is all very nice. I know what a null pointer reference is, and thank you for the compiler notes. Now I'm a little fuzzy on the idea of a literal in Java so I read on...
A literal is the source code
representation of a fixed value;
literals are represented directly in
your code without requiring
computation.There's also a special null literal
that can be used as a value for any
reference type. null may be assigned
to any variable, except variables of
primitive types. There's little you
can do with a null value beyond
testing for its presence. Therefore,
null is often used in programs as a
marker to indicate that some object is
unavailable.
Ok, so I think I get it now. In C++ NULL is a macro that, when compiled, evaluates to the null pointer constant. In Java, null is a fixed value that any non-primitive can be assigned too; great for testing in a handy if statement
.
Java does not have pointers, so I can see why they kept null a simple value rather than anything fancy. But why did java decide to change the all caps NULL
to null
?
Furthermore, am I missing anything here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
NULL
是一个预处理器指令标识符,根据约定,这些应该全部大写。null
是表示常量值的语言文字,并且根据约定应全部较低(就像true
或false
)。NULL
is a preprocessor directive identifier, according to convention, those should be all caps.null
is a language litteral representing a constant value and should according to convention be all lower (just astrue
orfalse
).Java 的 null 更像是 C++0x 的 nullptr。 C++ 中的 NULL 只是 0,最终可能会解析为 int 而不是您想要的指针。考虑一下:
C++0x 有 nullptr,它解决了这个问题,但它仍然不完全等同于 Java 的 null。它们只是不同的语言。
哦,另一个区别是 Java 没有指针(或者它是这么说的)。在 Java 中,您可以合法地将 null 分配给引用,而在 C++ 中,如果没有使用格式错误的构造,则无法这样做。诚然,如果没有这种能力,Java 几乎毫无用处,但这绝对是另一个重要的区别。
Java's null is more like C++0x's nullptr. NULL in C++ is just 0 and can end up resolving to int rather than a pointer like you'd want. Consider:
C++0x has nullptr, which fixes that problem but it's still not going to be totally equivalent to Java's null. They're just different languages.
Oh, and another diff is that Java has no pointers (or so it says). In Java you can legitimately assign null to a reference, in C++ you can't do that without having already used an ill-formed construct. Admittedly, Java would be next to useless without this ability but it's definitely another important difference.
关于你的最后一个问题,我不确定小写 null 背后的设计原理是什么,
但我认为目标是使 null 成为语言级别的文字(如 true 或 false)而不是常量。
java 中的关键字通常是小写的,因此被命名为文字。常量(如 Integer.MAX_VALUE)通常是大写的。
Regarding your last question, I'm not sure what the design rationale is behind lowercase null,
but I think that the goal was to make null a language-level literal (like true or false) rather than a constant.
Keywords in java are generally lowercased and so are named literals. Constants (like Integer.MAX_VALUE) are typically uppercased.
不,你已经差不多明白了。在 C++ 中,空指针是分配给指针的任何文字零值。
NULL
只是0
的宏,因此ptr = 0
和ptr = NULL
是相同的。包含它只是为了方便/可读。之所以是大写,是因为 C 和 C++ 中有一个长期约定,即宏名称应该大写。在 Java 中,
null
是一个内置文字,并且由于它不是宏(而且 Java 无论如何也没有宏的概念),因此没有令人信服的理由将其设为大写。No, you've pretty much got it. In C++, a null pointer is any literal zero value that is assigned to a pointer.
NULL
is just a macro for0
, and soptr = 0
andptr = NULL
are identical. It's included only for convenience/readability. The reason it is uppercase is because there is a longstanding convention in C and C++ that names of macros should be uppercase.In Java,
null
is a built-in literal, and since it is not a macro (and Java has no concept of a macro anyway), there is no compelling reason to make it uppercase.使用
0
而不是NULL
。摘自该语言的创建者 Bjarne Stroustrup 所著的《C++ 编程语言》一书:
Use
0
instead ofNULL
.From "The C++ Programming Language" book by the creator of the language, Bjarne Stroustrup:
Java null 关键字用于将变量标识为未引用任何对象。不能将 null 关键字分配给使用基本数据类型声明的变量。 C++ NULL 值是一个定义为 0 的常量。
The Java null keyword is used to identify a variable as not referencing any object. The null keyword cannot be assigned to a variable that is declared with a primitive data type. The C++ NULL value is a constant that is defined as 0.
这就是你搞错的地方。在 C++ 中,NULL 是 0,没有其他值。指针和整数都可以为 NULL。
另一方面,在java中,null是一个空指针,意味着只有对象可以为空。
This is where you got it wrong. In C++, NULL is 0, and nothing else. Both pointers and integers can be NULL.
In java on the other hand, null is a null pointer, meaning that only objects can be null.
正如您自己所说,Java没有指针,这就是为什么“null”的概念在Java和C++之间完全无法比较的原因。
你的问题就像“角度和苹果之间有什么区别”一样有意义,即除了明显的:C++中的
NULL
和null之外,它不能以任何其他方式回答。
在Java中是两个完全不相关的概念。Java 从未“决定”将所有大写
NULL
更改为null
,因为 Java 的null
与 C++ 的没有任何关系空。
Java doesn't have pointers, as you said yourself, which is why the concept of "null" is completely incomparable between Java and C++.
Your question makes as much sense as "what is the difference between an angle and an apple", i.e. it can't be answered in any other way besides the obvious:
NULL
in C++ andnull
in Java are two totally and completely unrelated concepts.Java never "decided" to change all caps
NULL
tonull
, because, once again, Java'snull
has no relation whatsoever to C++'sNULL
.好吧,我不知道为什么java决定将NULL更改为null,但是大写字母输入起来更麻烦(至少在我看来)。但你也许总是可以问 sun :-P。无论如何,说 java 没有指针并不完全正确,因为 java 或 c# 等中的普通引用更像指针,而不是 c++ 中的引用。例如,在 c++ 中,引用不能为 NULL,但在 java 中,引用可以。 C++ 中的引用不能更改它所指向的变量(在这一点上我可能是错误的),但 java 引用可以。
Well, I don't know WHY java desided to change NULL to null, however uppercase is more troublesom to type (at least in my consideration). But you can probably always ask sun :-P. Anyway, saying that java doesn't have pointers isn't compleately true, because normal references in java or c# or the likes works more like pointers than refferences in c++. For instance, in c++ a reference can't be NULL, but a reference in java can. And a reference in c++ can't change what variable it's pointing at (I might be mistaken at this point), but java reference can.
指针基本上是内存地址。并且只有一个众所周知且已建立的内存地址,0。c++ NULL 是大写的,因为它是一个预处理器宏,并且按照惯例它们总是大写。指针概念直接映射到硬件中地址空间的概念。
Java 使用引用,并且有自己的内部表示方式。有一个众所周知的参考文献:null。它是小写的,因为它基本上是另一个符号,尽管指的是字面值。我不认为他们从 C 到 Java “改变”了任何东西。我认为他们只是想要空概念,所以他们称之为“空”。
在这两种情况下,null 都允许函数返回附加信息。它可以返回结果的地址以及操作可能不成功的事实。
Pointers are basically memory addresses. And there is only one well-known and established memory address, 0. The c++ NULL is capitalized because it is a preprocessor macro and they are always capitalized by convention. The pointer concept maps directly to the concept of an address space in hardware.
Java uses references and it has it's own internal way of representing them. There is one well-known reference, null. It's in lower case because it is basically another symbol, albeit referring to a literal value.. I don't think they "changed" anything from C to Java. I think they just wanted the null concept so they called it "null".
In both cases, the null allows a function to return additional information. it can return the address of the result and the fact that maybe the operation was unsuccessful.