tags:

views:

454

answers:

10

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?

+12  A: 

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.

Noah Roberts
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
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.
AndreyT
@Tyler - it seems to me that they're more like pointers than references for exactly the reason that they can be null. Really the only diff between java refs and c++ ptrs is that you can't do math on them. Of course, AFAIK Java doesn't actually treat them like different types at all, reference or pointer, while C++ indeed does. In reality the two languages are hardly similar at all.
Noah Roberts
+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
@Tyler: Java references have reference syntax, in that you don't have to explicitly dereference a pointer and can't add or subtract from it, but the semantics are like a pointer, in that you can subsequently point them to anything or nothing. C# references are like this, too, but there's also a mode allowing genuine pointers, complete with pointer arithmetic and explicit dereferencing.
Steven Sudit
@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.
Noah Roberts
Saying Java has "no pointers" is a little disingenious, and technically incorrect. In Java, everything (except primitives) is a pointer.
aperkins
LOL! Everyone here is getting pissy about whether Java has pointers or not. Either one way or the other someone's going to have their religious values offended. Frankly, I'm not one to care about such silly sensibilities...especially since, as you'll note, I left a caveat in my post about this very thing. Been negged 3 times on this post and I'll bet good money that they're all about the pointer issue - on both sides. You guys should move to Iraq and battle it out there.
Noah Roberts
@Noah still a great answer. I've been reading about C++Ox. Fun stuff.
Stephano
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
+13  A: 
  • 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).

aioobe
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
@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".
mos
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
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
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
+3  A: 

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;

metaliving
Wow, very interesting. So can I say something like: (class) Node n = 0; ?
Stephano
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
Right, the C++ NULL is 0, whereas the C NULL is more often (void*)0.
Steven Sudit
+1 for teaching me something very cool. I had no idea that Node* n = 0; was valid.
Stephano
@Metaliving I suppose you mean casting (= implicitly converting) an integer to a Node, in case the * is omitted.
starblue
Yeah, that's right. Sorry for not being clear enough.
metaliving
+1  A: 

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.

Viktor Sehr
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
That was poor word choice. I've changed it to "evaluates to".
Stephano
@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.
Noah Roberts
A: 

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.

Alxandr
+2  A: 

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.

Tyler McHenry
+4  A: 

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.

Uri
+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
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
A: 

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.

AndreyT
From another point of view, Java is mostly pointers, and therefore the concepts are very close.
David Thornley
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
A: 

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.

Vagrant
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
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
+1  A: 

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.

pcent