Is null an instance of anything?

No, there is no type which null is an instanceof.

15.20.2 Type Comparison Operator instanceof

RelationalExpression:
    RelationalExpression instanceof ReferenceType

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

This means that for any type E and R, for any E o, where o == null, o instanceof R is always false.


What set does 'null' belong to?

JLS 4.1 The Kinds of Types and Values

There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.


What is null?

As the JLS quote above says, in practice you can simply pretend that it's "merely a special literal that can be of any reference type".

In Java, null == null (this isn't always the case in other languages). Note also that by contract, it also has this special property (from java.lang.Object):

public boolean equals(Object obj)

For any non-null reference value x, x.equals(null) should return false.

It is also the default value (for variables that have them) for all reference types:

JLS 4.12.5 Initial Values of Variables

  • Each class variable, instance variable, or array component is initialized with a default value when it is created:
    • For all reference types, the default value is null.

How this is used varies. You can use it to enable what is called lazy initialization of fields, where a field would have its initial value of null until it's actually used, where it's replaced by the "real" value (which may be expensive to compute).

There are also other uses. Let's take a real example from java.lang.System:

public static Console console()

Returns: The system console, if any, otherwise null.

This is a very common use pattern: null is used to denote non-existence of an object.

Here's another usage example, this time from java.io.BufferedReader:

public String readLine() throws IOException

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

So here, readLine() would return instanceof String for each line, until it finally returns a null to signify the end. This allows you to process each line as follows:

String line;
while ((line = reader.readLine()) != null) {
   process(line);
}

One can design the API so that the termination condition doesn't depend on readLine() returning null, but one can see that this design has the benefit of making things concise. Note that there is no problem with empty lines, because an empty line "" != null.

Let's take another example, this time from java.util.Map<K,V>:

V get(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Here we start to see how using null can complicate things. The first statement says that if the key isn't mapped, null is returned. The second statement says that even if the key is mapped, null can also be returned.

In contrast, java.util.Hashtable keeps things simpler by not permitting null keys and values; its V get(Object key), if returns null, unambiguously means that the key isn't mapped.

You can read through the rest of the APIs and find where and how null is used. Do keep in mind that they aren't always the best practice examples.

Generally speaking, null are used as a special value to signify:

  • Uninitialized state
  • Termination condition
  • Non-existing object
  • An unknown value

How is it represented in the memory?

In Java? None of your concern. And it's best kept that way.


Is null a good thing?

This is now borderline subjective. Some people say that null causes many programmer errors that could've been avoided. Some say that in a language that catches NullPointerException like Java, it's good to use it because you will fail-fast on programmer errors. Some people avoid null by using Null object pattern, etc.

This is a huge topic on its own, so it's best discussed as answer to another question.

I will end this with a quote from the inventor of null himself, C.A.R Hoare (of quicksort fame):

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.

The video of this presentation goes deeper; it's a recommended watch.

Answer from polygenelubricants on Stack Overflow
Top answer
1 of 14
331

Is null an instance of anything?

No, there is no type which null is an instanceof.

15.20.2 Type Comparison Operator instanceof

RelationalExpression:
    RelationalExpression instanceof ReferenceType

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

This means that for any type E and R, for any E o, where o == null, o instanceof R is always false.


What set does 'null' belong to?

JLS 4.1 The Kinds of Types and Values

There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.


What is null?

As the JLS quote above says, in practice you can simply pretend that it's "merely a special literal that can be of any reference type".

In Java, null == null (this isn't always the case in other languages). Note also that by contract, it also has this special property (from java.lang.Object):

public boolean equals(Object obj)

For any non-null reference value x, x.equals(null) should return false.

It is also the default value (for variables that have them) for all reference types:

JLS 4.12.5 Initial Values of Variables

  • Each class variable, instance variable, or array component is initialized with a default value when it is created:
    • For all reference types, the default value is null.

How this is used varies. You can use it to enable what is called lazy initialization of fields, where a field would have its initial value of null until it's actually used, where it's replaced by the "real" value (which may be expensive to compute).

There are also other uses. Let's take a real example from java.lang.System:

public static Console console()

Returns: The system console, if any, otherwise null.

This is a very common use pattern: null is used to denote non-existence of an object.

Here's another usage example, this time from java.io.BufferedReader:

public String readLine() throws IOException

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

So here, readLine() would return instanceof String for each line, until it finally returns a null to signify the end. This allows you to process each line as follows:

String line;
while ((line = reader.readLine()) != null) {
   process(line);
}

One can design the API so that the termination condition doesn't depend on readLine() returning null, but one can see that this design has the benefit of making things concise. Note that there is no problem with empty lines, because an empty line "" != null.

Let's take another example, this time from java.util.Map<K,V>:

V get(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

If this map permits null values, then a return value of null does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinguish these two cases.

Here we start to see how using null can complicate things. The first statement says that if the key isn't mapped, null is returned. The second statement says that even if the key is mapped, null can also be returned.

In contrast, java.util.Hashtable keeps things simpler by not permitting null keys and values; its V get(Object key), if returns null, unambiguously means that the key isn't mapped.

You can read through the rest of the APIs and find where and how null is used. Do keep in mind that they aren't always the best practice examples.

Generally speaking, null are used as a special value to signify:

  • Uninitialized state
  • Termination condition
  • Non-existing object
  • An unknown value

How is it represented in the memory?

In Java? None of your concern. And it's best kept that way.


Is null a good thing?

This is now borderline subjective. Some people say that null causes many programmer errors that could've been avoided. Some say that in a language that catches NullPointerException like Java, it's good to use it because you will fail-fast on programmer errors. Some people avoid null by using Null object pattern, etc.

This is a huge topic on its own, so it's best discussed as answer to another question.

I will end this with a quote from the inventor of null himself, C.A.R Hoare (of quicksort fame):

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.

The video of this presentation goes deeper; it's a recommended watch.

2 of 14
33

Is null an instance of anything?

No. That is why null instanceof X will return false for all classes X. (Don't be fooled by the fact that you can assign null to a variable whose type is an object type. Strictly speaking, the assignment involves an implicit type conversion; see below.)

What set does 'null' belong to?

It is the one and only member of the null type, where the null type is defined as follows:

"There is also a special null type, the type of the expression null, which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type." JLS 4.1

What is null?

See above. In some contexts, null is used to denote "no object" or "unknown" or "unavailable", but these meanings are application specific.

How is it represented in the memory?

That is implementation specific, and you won't be able to see the representation of null in a pure Java program. (But null is represented as a zero machine address / pointer in most if not all Java implementations.)

🌐
Baeldung
baeldung.com › home › java › what is the null type in java?
What Is the null Type in Java? | Baeldung
January 8, 2024 - If we want to declare an int variable, the set of possible values would be much larger but still clearly defined: every possible number from -2^31 to 2^31-1. null is a special type that has only one possible value.
🌐
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).
🌐
Quora
quora.com › What-is-the-data-type-of-null-in-Java
What is the data type of null in Java? - Quora
Answer (1 of 5): In Java You have different primitive data types byte , short , int , long , float , double , boolean and char. And Non-primitive data types - such as String, Arrays and Classes. Data types are ways to represent a specific type of data in memory. They have sizes and eventually ca...
🌐
Logit
logit.io › blog › post › null-in-java
The Concept Of Null In Java
February 4, 2025 - The main difference between primitive and reference type is that primitive type always has a value, it can never be null but reference type can be null, which denotes the absence of value.
🌐
DEV Community
dev.to › dj_devjournal › understanding-null-in-java-4o31
Understanding null in Java - DEV Community
October 16, 2019 - The output of the above code snippet is shown in Figure 1 shown below. Figure 1: OUTPUT of the code snippet written above. In Java, null is a reserved word (keyword) for literal values.
🌐
GeeksforGeeks
geeksforgeeks.org › java › interesting-facts-about-null-in-java
Interesting facts about null in Java - GeeksforGeeks
September 3, 2024 - An empty string is a string that contains no characters, while an empty array is an array that contains no elements. The Java programming language has a built-in null type, called "null", which is a subtype of all reference types.
🌐
SAS
support.sas.com › rnd › itech › doc9 › dev_guide › dist-obj › javaclnt › javaprog › javastub › nullref.html
Null References
In Java programming, null can be assigned to any variable of a reference type (that is, a non-primitive type) to indicate that the variable does not refer to any object or array. CORBA also allows null object references, but it is important to note that not all Java reference types map to CORBA ...
🌐
OpenJDK
openjdk.org › jeps › 8303099
JEP draft: Null-Restricted and Nullable Types (Preview)
It is not a goal to make any changes to the primitive types, such as adding support for a nullable int type · It is not a goal (at this time) to apply the language enhancements to the standard libraries · In a Java program, a variable of type String may hold either a reference to a String ...
Find elsewhere
🌐
DataFlair
data-flair.training › blogs › java-null
Java Null - 7 Unknown Facts about Null in Java - DataFlair
May 2, 2024 - This means that you cannot expect NULL to be a “literal” in Java. It’s the same as true and false. These have individual meaning. All reference variables have null as their default value. The compiler is unable to unbox null objects. It throws a NullPointerException. Null is not an instance of any class. Hence a null value will return false if used with the instanceOf operator. Static methods are callable with a reference of the null type.
🌐
Blogger
javarevisited.blogspot.com › 2014 › 12 › 9-things-about-null-in-java.html
9 Things about null keyword and reference in Java
First thing, first, null is a keyword in Java, much like public, static or final. It's case sensitive, you cannot write null as Null or NULL, compiler will not recognize them and give error.
Top answer
1 of 4
11

null is actually not instanceof anything!

The instanceof operator from the Java Language Specification (§15.20.2):

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast (§15.16) to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

4.1. The Kinds of Types and Values

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

Type: PrimitiveType ReferenceType There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.

The null reference is the only possible value of an expression of null type.

The null reference can always undergo a widening reference conversion to any reference type.

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

2 of 4
10

Formally, null is a singleton member of the null type, which is defined to be the subtype of every other Java type.

null is a reference type and its value is the only reference value which doesn't refer to any object. Therefore there is no representation of null in memory. The binary value of a reference-typed variable whose value is null is simply zero (all zero bits). Even though this is not explicitly specified, it follows from the general initialization semantics of objects and any other value would cause major problems to an implementation.

🌐
Coderanch
coderanch.com › t › 688734 › java › null
what does null mean? (Beginning Java forum at Coderanch)
Null is the lack of anything. A value hasn't been assigned to a saved space, so it doesn't have anything it can output or use for any other function. ... Null's an odd, weird, annoying, type of non type thing. I believe its a type of non type in the java specification too!
🌐
Maroontress
maroontress.github.io › The-Insanity-Beyond-Null › Part-1.html
Part 1. Null in Java 11 | The Insanity Beyond Null
In Java, primitive values such as int and boolean are never null. So we only need to consider the values of the reference types. However, you should be careful with boxing/unboxing primitive types to/from their wrapper-classes such as Integer and Boolean. Consider an example that when a key is pressed, the action associated with the key is performed:
🌐
OpenJDK
openjdk.org › jeps › 8316779
JEP draft: Null-Restricted Value Class Types (Preview)
The features described below are ... libraries can be found in subtasks of this JEP. A null-restricted type is a reference type expressed with the name of a value class followed by the !...
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8303099
[JDK-8303099] Null-Restricted and Nullable Types (Preview)
It is not a goal to make any changes to the primitive types, such as adding support for a nullable int type · It is not a goal (at this time) to apply the language enhancements to the standard libraries · In a Java program, a variable of type String may hold either a reference to a String ...
🌐
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 - Note: Unfortunately most popular programming languages don’t distinguish between nullable and non-nullable types. There is no way to reliably state that null can never be assigned to a given object reference. In some languages it is possible to use annotations, such as the non-standard annotations @Nullable and @NonNullable in Java.
🌐
DataCamp
datacamp.com › doc › java › null
null Keyword in Java: Usage & Examples
The null keyword in Java is a literal that represents a null reference, one that points to no object. It is often used to indicate that a reference variable does not currently refer to any object or that a method has no return value. The null keyword can be assigned to any reference type variable, ...
🌐
Reddit
reddit.com › r/java › java might eventually get null-restricted types
r/java on Reddit: Java might eventually get null-restricted types
April 24, 2023 - In java, basic types are inviolable (a method declared to return String cannot possibly give you an Integer; the compiler and class verifier guarantee this). But generics are not. It is looking like the nullness bit needs to be erased for identity-class types, and needs to be not-erased for value-class types.
Top answer
1 of 2
7

It's a reference value, not a reference variable. Any variable that is a reference type can have the value null. (In Java, all variables are either reference types or primitive types (int, char, float, etc.). (Well, there are also type variables when you start to talk about generics.)

Here's the relevant part of the Java Language Specification:

There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3). There are, correspondingly, two kinds of data values that can be stored in variables, passed as arguments, returned by methods, and operated on: primitive values (§4.2) and reference values (§4.3).

There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name.

Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type.

The null reference is the only possible value of an expression of null type.

The null reference can always undergo a widening reference conversion to any reference type.

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

EDIT To address your comment: I think the language in "The Java Handbook" is a little off the mark. The value null is not a reference of type Object; it is a reference of the null type. The key point from the spec is that "the null reference can always undergo a widening reference conversion to any reference type". This is, in a sense, exactly the opposite of Object. An Object reference is the widest type of reference; the null type is (to also speak a little loosely) the narrowest. In particular, assigning a reference of type Object to a variable of any other reference type is a narrowing conversion that requires an explicit cast (and can raise a ClassCastException). Assigning the null reference to a variable of any reference type never requires a cast and cannot raise an exception.

Note that no named reference type can have the behavior of the null type. There really is no "narrowest type" since the (named) type system in Java does not not have such a thing. It is impossible to define a reference type that is assignable to, say, both a String variable and a Double variable. Only the null type has that property. The normal rules for reference type conversion do not allow this, which is why it has a separate rule in the Java Language Specification.

2 of 2
1

From JLS:

The null type has one value, the null reference, represented by the null literal null, which is formed from ASCII characters.