C++ 中的 NULL 和 Java 中的 null 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2960193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:41:47  来源:igfitidea点击:

What is the difference between NULL in C++ and null in Java?

javac++null

提问by Stephano

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.

我一直在试图弄清楚为什么 C++ 让我疯狂打字NULL。前几天突然它袭击了我;多年来,我一直null在用 Java打字(小写)。现在突然我在用 C++ 编程,那一小块肌肉记忆让我发疯。

Wikiperipatetic defines C++ NULL as part of the stddef:

Wikiperipatetic 将 C++ NULL 定义为 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.

扩展为空指针常量的宏。它可能被定义为 ((void*)0)、0 或 0L,具体取决于编译器和语言。

Sun's docs tells me this about Java's "null literal":

Sun 的文档告诉我有关 Java 的“空文字”的内容:

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.

null 类型有一个值,即 null 引用,由字面量 null 表示,它由 ASCII 字符组成。空字面量始终为空类型。

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...

所以这一切都很好。我知道空指针引用是什么,感谢您的编译器注释。现在我对 Java 中文字的概念有点模糊,所以我继续阅读......

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.

文字是固定值的源代码表示;文字直接在您的代码中表示,无需计算。

还有一个特殊的空文字可以用作任何引用类型的值。null 可以分配给任何变量,原始类型的变量除外。除了测试空值的存在之外,您几乎无能为力。因此,程序中经常使用 null 作为标记,以指示某些对象不可用。

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.

好的,所以我想我现在明白了。在 C++ 中,NULL 是一个宏,它在编译时计算为空指针常量。在 Java 中,null 是一个固定值,任何非原始值也可以赋值;非常适合在方便的情况下进行测试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 NULLto null?

Java 没有指针,所以我可以理解为什么他们将 null 保持为一个简单的值而不是任何花哨的值。 但是为什么 java 决定将全部大写更改NULLnull

Furthermore, am I missing anything here?

此外,我在这里遗漏了什么吗?

回答by aioobe

  • NULLis a preprocessor directive identifier, according to convention, those should be all caps.

  • nullis a language litteralrepresenting a constant value and should according to convention be all lower (just as trueor false).

  • NULL是一个预处理器指令标识符,按照惯例,那些应该全部大写。

  • null是一种表示常量值的语言文字,按照惯例应该都更低(就像truefalse)。

回答by Edward Strange

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:

Java 的 null 更像是 C++0x 的 nullptr。C++ 中的 NULL 只是 0,最终可以解析为 int 而不是您想要的指针。考虑:


void f(int);
void f(char*);

...

f(NULL); // which f()?

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.

C++0x 有 nullptr,它解决了这个问题,但它仍然不会完全等同于 Java 的 null。他们只是不同的语言。

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.

哦,另一个区别是 Java 没有指针(或者它是这么说的)。在 Java 中,您可以合法地将 null 分配给引用,而在 C++ 中,如果没有使用格式错误的构造,则无法这样做。诚然,如果没有这种能力,Java 几乎毫无用处,但这绝对是另一个重要的区别。

回答by Uri

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.

关于你的最后一个问题,我不确定小写 null 背后的设计原理是什么,但我认为目标是使 null 成为语言级文字(如 true 或 false)而不是常量。

Keywords in java are generally lowercased and so are named literals. Constants (like Integer.MAX_VALUE) are typically uppercased.

java中的关键字通常是小写的,命名文字也是如此。常量(如 Integer.MAX_VALUE)通常是大写的。

回答by metaliving

Use 0instead of NULL.

使用0代替NULL

From "The C++ Programming Language" book by the creator of the language, Bjarne Stroustrup:

来自该语言创建者 Bjarne Stroustrup 的“The C++ Programming Language”一书:

In C, it has been popular to define a macro NULLto represent the zero pointer. Because of C++'s tighter type checking, the use of plain 0, rather than any suggested NULLmacro, leads to fewer problems. If you feel you must define NULL, use const int NULL = 0;

在 C 中,定义一个宏NULL来表示零指针已经很流行了。由于 C++ 更严格的类型检查,使用 plain0而不是任何建议的NULL宏会导致更少的问题。如果您觉得必须定义NULL,请使用const int NULL = 0;

回答by Tyler McHenry

No, you've pretty much got it. In C++, a null pointer is any literal zero value that is assigned to a pointer. NULLis just a macro for 0, and so ptr = 0and ptr = NULLare 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.

不,你已经明白了。在 C++ 中,空指针是分配给指针的任何文字零值。NULL只是 的一个宏0,因此ptr = 0ptr = NULL是相同的。包含它只是为了方便/可读性。它是大写的原因是因为在 C 和 C++ 中有一个长期约定,即宏的名称应该是大写的。

In Java, nullis 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.

在 Java 中,null是内置文字,并且由于它不是宏(而且 Java 无论如何都没有宏的概念),因此没有令人信服的理由将其设为大写。

回答by pcent

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.

Java null 关键字用于将变量标识为未引用任何对象。不能将 null 关键字分配给使用原始数据类型声明的变量。C++ NULL 值是定义为 0 的常量。

回答by Viktor Sehr

In C++ NULL is a macro that, when compiled, defines the null pointer constant.

在 C++ 中,NULL 是一个宏,它在编译时定义了空指针常量。

This is where you got it wrong. In C++, NULL is 0, and nothing else. Both pointers and integers can be NULL.

这就是你弄错的地方。在 C++ 中,NULL 是 0,没有别的。指针和整数都可以为 NULL。

In java on the other hand, null is a null pointer, meaning that only objects can be null.

另一方面,在 Java 中,null 是一个空指针,这意味着只有对象才能为空。

回答by AnT

Java doesn't have pointers, as you said yourself, which is why the concept of "null" is completely incomparable between Java and C++.

Java没有指针,正如你自己所说的,这就是为什么“空”的概念在Java和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: NULLin C++ and nullin Java are two totally and completely unrelated concepts.

您的问题与“角度和苹果之间有什么区别”一样有意义,即除了显而易见的问题之外,无法以任何其他方式回答:NULL在 C++ 和nullJava 中是两个完全不相关的概念。

Java never "decided" to change all caps NULLto null, because, once again, Java's nullhas no relation whatsoever to C++'s NULL.

Java 从未“决定”将所有大写字母更改NULLnull,因为再次重申,Javanull与 C++ 的NULL.

回答by Vagrant

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.

指针基本上是内存地址。并且只有一个众所周知的已建立的内存地址,0。c++ NULL 是大写的,因为它是一个预处理器宏,并且它们总是按照惯例大写。指针概念直接映射到硬件中地址空间的概念。

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".

Java 使用引用并且它有自己的内部方式来表示它们。有一个众所周知的引用,null。它是小写的,因为它基本上是另一个符号,尽管指的是文字值..我认为他们没有将任何东西从 C 更改为 Java。我认为他们只是想要空概念,所以他们称之为“空”。

In both cases, the null allows a function to return additional information. it can return the address of the result andthe fact that maybe the operation was unsuccessful.

在这两种情况下,null 都允许函数返回附加信息。它可以返回结果的地址可能操作不成功的事实。

回答by Alxandr

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.

好吧,我不知道为什么 java 想要将 NULL 更改为 null,但是大写输入起来更麻烦(至少在我的考虑中)。但是你可能总是问sun :-P。无论如何,说 java 没有指针并不完全正确,因为 java 或 c# 等中的普通引用更像是指针而不是 c++ 中的引用。例如,在 c++ 中,引用不能为 NULL,但在 java 中的引用可以。C++ 中的引用不能改变它指向的变量(我可能在这一点上错了),但 java 引用可以。