Take the 2-minute tour ×

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?

share|improve this question
"Java does not have pointers". Actually, Java only has pointers. –  Viktor Sehr Jun 2 '10 at 18:01
Except for basic types =) –  Viktor Sehr Jun 2 '10 at 18:02
((void*)0) isn't a valid definition of NULL in C++. –  Mike Seymour Jun 2 '10 at 18:07
@Viktor Sehr: Not true. Java has no pointers. Even if some implementation (or even all of them) decided to implement the concept of Java object through physical pointers, still does not mean that in Java objects are pointers. –  AnT Jun 2 '10 at 18:10
@AndreyT. Java objects are obviously not pointers. Java objects are java objects. What @Viktor means is that one can't store an objects in variables, but only references to objects, which is true. –  aioobe Jun 2 '10 at 18:22

10 Answers 10

up vote 19 down vote accepted

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:

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.

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.

share|improve this answer
Java references have some of the properties of C++ pointers and some of the properties of C++ references, so it's hard to compare them directly. –  Tyler McHenry Jun 2 '10 at 18:03
No, Java's null has absolutely no similarity to C++ nullptr simply because Java has no pointers, and C++'s nullptr is strictly pointer-specific. –  AnT Jun 2 '10 at 18:04
+1 Great answer, though, you missed the part about the capital letters. Anyhoo, I think people are generally less interested in that. I'm clearly an odd ball. –  Stephano Jun 2 '10 at 18:21
@Stephano - yeah, I did. I don't really know why Sun did that. Some of the other answers here make sense but in the end I think it's all just guessing. Nobody would really know except Sun unless we could find it documented somewhere. –  Crazy Eddie Jun 2 '10 at 18:29
I had no idea I was opening up a "Java pointers yes/no" feud. I agree with Noah. That's off topic. Don't downvote people who are answering questions but offending your sensitive code views. –  Stephano Jun 2 '10 at 18:51
  • 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 as true or false).

share|improve this answer
Well met and good answer. Now, any idea why they have this convention? You know they had to be sitting around a table talking about true/false/null and thinking... "hmm, we are changing null on them..." –  Stephano Jun 2 '10 at 18:03
@Stephano They likely didn't sit around talking about it. "true" and "false" are all lowercase in C++, as well. It's a pretty common convention. Once Java decided that the concept of "no object here" was a built-in constant, it needed to follow convention. Hence, "null". –  moswald Jun 2 '10 at 18:11
Can't really think of any good answer, except that all keywords are lower-case. Perhaps they simply wanted to emphasis that it was not a macro of any kind, and actually part of the language spec. To be honest, I don't think they thought of it as a "change" from C++ NULL. –  aioobe Jun 2 '10 at 18:12
I think I'm just a loon. I picture Bjarne in his PJ's one morning in the late 90's trying to write an if(foo == NULL) and cursing Java for its crazy lower case –  Stephano Jun 2 '10 at 18:34
I always assumed that they were capitalized to let you know that they were not part of the language, were literally replaced before compiler even saw then, and might have side-effects. For instance MAX(a,++b) might not do what you intend if "b++" is placed twice by the preprocessor. The caps convention serves as a warning, in my mind, and a hint on where to look for the definition (/include instead of /lib). –  Vagrant Jun 2 '10 at 18:36

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.

share|improve this answer
+1 Also a good answer. I had to go look up a bunch of constants, but it would seem they are mostly upper case. Interesting point. –  Stephano Jun 2 '10 at 18:24
As commentary, I'd like to point out that C had no true or false, but header files would typically #define TRUE 1 and #define FALSE 0. The C NULL fits this pattern. –  Steven Sudit Jun 2 '10 at 18:24

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 for 0, and so ptr = 0 and ptr = 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.

share|improve this answer

Use 0 instead of NULL.

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

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

share|improve this answer
Wow, very interesting. So can I say something like: (class) Node n = 0; ? –  Stephano Jun 2 '10 at 18:17
It would have to be Node *n = 0, because 0 is a (null) pointer. In other words, since no object can have an address in memory 0, 0 in that case would indicate that the pointer points to no instance of the Node class. Otherwise, it'd look like you're trying to cast a Node to an integer. –  metaliving Jun 2 '10 at 18:22
Right, the C++ NULL is 0, whereas the C NULL is more often (void*)0. –  Steven Sudit Jun 2 '10 at 18:26
+1 for teaching me something very cool. I had no idea that Node* n = 0; was valid. –  Stephano Jun 2 '10 at 18:30
@Metaliving I suppose you mean casting (= implicitly converting) an integer to a Node, in case the * is omitted. –  starblue Jun 2 '10 at 19:18

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.

share|improve this answer

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

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.

share|improve this answer
I wouldn't say it's wrong. NULL is a macro that evaluates to the null pointer constant 0. It doesn't define the null pointer context, but I think that's just a matter of ambiguous word choice. –  Tyler McHenry Jun 2 '10 at 18:02
That was poor word choice. I've changed it to "evaluates to". –  Stephano Jun 2 '10 at 18:05
@both comments - This poster is correct and it's an important thing to note. NULL is not a pointer constant, it is an integral constant. It happens to be that the value that NULL is, 0, also evaluates to a null pointer when used in the context of a pointer value. As I mention in my own post though, NULL will resolve to integer before pointer because that is what it actually is. –  Crazy Eddie Jun 2 '10 at 18:15

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++ and null in Java are two totally and completely unrelated concepts.

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

share|improve this answer
From another point of view, Java is mostly pointers, and therefore the concepts are very close. –  David Thornley Jun 2 '10 at 18:11
Andrey: That's strictly true, but practically false. You're right that Java lacks pointers, as such, but its references are very much like C/C++ pointers. –  Steven Sudit Jun 2 '10 at 18:29

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.

share|improve this answer

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.

share|improve this answer
The use of 0 for the null pointer in C and C++ is not based on the idea that memory location 0 is special, although that's how it's usually implemented. A constant integral 0 will be cast to the null pointer constant. –  David Thornley Jun 2 '10 at 18:10
I'm not saying that memory location 0 is special, just that it is well-known and used as a convention to say that the pointer is not a valid address. –  Vagrant Jun 2 '10 at 18:32

Your Answer


