This was intended as a meme but is actually a good representation of what "Null" is. In C#, when you declare string s = "My shit"; it means that "s" is a reference to a memory location that holds the data "My shit". string s = null; means that the reference "s" exists but it's not pointing to any object, as in it holds nothing. Answer from abd53 on reddit.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Null
Null - Glossary | MDN
In computer science, a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address. The meaning of a null reference varies among language implementations.
🌐
ThoughtCo
thoughtco.com › definition-of-null-958118
What Does Null Mean in C, C++ and C#?
April 27, 2019 - In a database, zero is a value. The value null means that no value exists. When used as a value, null is not a memory location. Only pointers hold memory locations. Without a null character, a string would not correctly terminate, which would ...
🌐
freeCodeCamp
freecodecamp.org › news › a-quick-and-thorough-guide-to-null-what-it-is-and-how-you-should-use-it-d170cea62840
A quick and thorough guide to ‘null’: what it is, and how you should use it
June 12, 2018 - As we can see, in case of a value type, the value itself is directly stored at the address A0A1 which is associated with variable name. There would be much more to say about reference versus value types, but this is out of the scope of this article. Please note also that some programming languages support only reference types, others support only value types, and some (e.g. C# and Java) support both of them. ... The concept of null exists only for reference types.
Top answer
1 of 16
49

Null: The Billion Dollar Mistake. Tony Hoare:

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years. In recent years, a number of program analysers like PREfix and PREfast in Microsoft have been used to check references, and give warnings if there is a risk they may be non-null. More recent programming languages like Spec# have introduced declarations for non-null references. This is the solution, which I rejected in 1965.

2 of 16
32

null is a sentinel value that is not an integer, not a string, not a boolean - not anything really, except something to hold and be a "not there" value. Don't treat it as or expect it to be a 0, or an empty string or an empty list. Those are all valid values and can be geniunely valid values in many circumstances - the idea of a null instead means there is no value there.

Perhaps it's a little bit like a function throwing an exception instead of returning a value. Except instead of manufacturing and returning an ordinary value with a special meaning, it returns a special value that already has a special meaning. If a language expects you to work with null, then you can't really ignore it.

🌐
Quora
quora.com › What-does-NULL-mean-in-programming-languages
What does NULL mean in programming languages? - Quora
Answer (1 of 6): null is the pointer or reference to address zero. Why would this be needed or used? Let’s say you are searching for a library book that you want to read. Unlike Amazon, your library doesn’t have every book.
🌐
Upwork
upwork.com › resources › articles › {name}
Null in Java: Understanding the Basics - Upwork
August 5, 2024 - 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 (a common misconception some newcomers to the Java language grapple with).
🌐
Khoury College of Computer Sciences
khoury.northeastern.edu › home › kenb › MeaningOfNull.html
The Meaning of Null in Databases and Programming Languages
The main distinction between the relational null and the programming language null is the following: The relational null represents the absence of a value in a field of a record; whereas the programming language null represents one of the possible values of a variable.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › keywords › null
null keyword - C# reference | Microsoft Learn
... The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null, except for nullable value types.
🌐
TechCareer
techcareer.net › en › dictionary › null
What is Null? What does it do?
Null, in many programming languages, means "empty" or "nothing." It is used to indicate that a variable has no value. Null shows that an object or reference does not contain any data or does not point to an object.
🌐
Wikipedia
en.wikipedia.org › wiki › Null
Null - Wikipedia
1 month ago - Null (SQL) (or NULL), a special marker and keyword in SQL indicating that a data value does not exist, is not known, or is missing.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › null
null - JavaScript | MDN
The keyword null is a literal for the null value. Unlike undefined, which is a global variable, null is not an identifier but a syntax keyword. ... Like undefined, accessing any property on null throws a TypeError instead of returning undefined or searching prototype chains.
Top answer
1 of 5
7

Actually, you can use a literal 0 anyplace you would use NULL.

Section 6.3.2.3p3 of the C standard states:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

And section 7.19p3 states:

The macros are:

NULL

which expands to an implementation-defined null pointer constant

So 0 qualifies as a null pointer constant, as does (void *)0 and NULL. The use of NULL is preferred however as it makes it more evident to the reader that a null pointer is being used and not the integer value 0.

2 of 5
5

NULL is used to make it clear it is a pointer type.

Ideally, the C implementation would define NULL as ((void *) 0) or something equivalent, and programmers would always use NULL when they want a null pointer constant.

If this is done, then, when a programmer has, for example, an int *x and accidentally writes *x = NULL;, then the compiler can recognize that a mistake has been made, because the left side of = has type int, and the right side has type void *, and this is not a proper combination for assignment.

In contrast, if the programmer accidentally writes *x = 0; instead of x = 0;, then the compiler cannot recognize this mistake, because the left side has type int, and the right side has type int, and that is a valid combination.

Thus, when NULL is defined well and is used, mistakes are detected earlier.

In particular answer to your question “Is there a context in which just plain literal 0 would not work exactly the same?”:

  • In correct code, NULL and 0 may be used interchangeably as null pointer constants.
  • 0 will function as an integer (non-pointer) constant, but NULL might not, depending on how the C implementation defines it.
  • For the purpose of detecting errors, NULL and 0 do not work exactly the same; using NULL with a good definition serves to help detect some mistakes that using 0 does not.

The C standard allows 0 to be used for null pointer constants for historic reasons. However, this is not beneficial except for allowing previously written code to compile in compilers using current C standards. New code should avoid using 0 as a null pointer constant.

🌐
GeeksforGeeks
geeksforgeeks.org › java › interesting-facts-about-null-in-java
Interesting facts about null in Java - GeeksforGeeks
September 3, 2024 - Below are some important points about null in java that every Java programmer should know: In Java, null is a special value that represents the absence of a value or reference.
🌐
TechTerms
techterms.com › definition › null
Null Definition - What does null mean to a computer?
September 26, 2022 - Null, in computing terms, refers to the absence of a value. It does not mean a value of 0, since 0 is itself a value, nor does it mean a blank space " ". It also does not refer to an undefined variable, which has not been created at all. In the context of programming languages, a variable that ...
🌐
DataCamp
datacamp.com › doc › java › null
null Keyword in Java: Usage & Examples
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming ... The null keyword in Java is a literal that represents a null reference, one that points to no object.
🌐
Programming Duck
programmingduck.com › home › articles › nulls and null checks – how to work safely with nulls in any codebase
Nulls and null checks - How to work safely with nulls in any codebase - Programming Duck
January 4, 2022 - The problem is that something in your code might be null when it’s not supposed to be. In other words, you have a bug. But, if you have null checks where they’re not needed, you’ll silently ignore the bug. It will be swallowed up in a null check. ... In the code above, car may be null when it’s not supposed to be. That’s a bug. However, due to an unnecessary null check, the program won’t crash.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
The Null Pointer is the pointer that does not point to any location but NULL. According to C11 standard: “An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.
Published   January 10, 2025
🌐
Esolang
esolangs.org › wiki › NULL
NULL - Esolang
NULL is a programming language in zero dimensions.
🌐
Zenva
gamedevacademy.org › home › software development › what is null in programming – complete guide
What Is Null In Programming - Complete Guide - GameDev Academy
November 21, 2023 - In programming, ‘null’ represents a null reference or a null pointer, which essentially means “no object” or “no value here”. It is a placeholder that signifies the absence of a value or a non-existent object.