Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 997
Stephano Avatar asked Jun 02 '10 17:06

Stephano


People also ask

What is the difference between NUL and null in C?

The C NUL is a single character that compares equal to 0. The C NULL is a special reserved pointer value that does not point to any valid data object. The SQL null value is a special value that is distinct from all non-null values and denotes the absence of a (non-null) value.

What is the difference between null and null in Java?

@arvin_codeHunk, null is an empty object, whereas "null" is an actual string containing the characters 'n', 'u', 'l', and 'l'.

Is null the same as in C?

It is the same as the character 0 used to terminate strings in C. Null can also be the value of a pointer, which is the same as zero unless the CPU supports a special bit pattern for a null pointer.

What is null in Java?

In Java, null is a literal, a special constant you can point to whenever you wish to point to the absence of a value. It is neither an object nor a type, which is a common misconception newcomers to the Java language have to grapple with.


2 Answers

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

like image 173
aioobe Avatar answered Sep 25 '22 06:09

aioobe


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.

like image 20
Edward Strange Avatar answered Sep 24 '22 06:09

Edward Strange