If null were an Object, it would support the methods of java.lang.Object such as equals(). However, this is not the case - any method invocation on a null results in a NullPointerException.

And this is what the Java Language Specification has to say on this topic:

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.

I think this can be boiled down to "null is special".

Answer from Michael Borgwardt on Stack Overflow
🌐
DZone
dzone.com › coding › languages › null object pattern in java
Null Object Pattern in Java
August 22, 2018 - A null object refers to an object without any reference or an object defined with neutral/null functionality/behavior. These null objects need to be checked to ensure that they are not null while accessing any member or invoking any methods.
Top answer
1 of 4
43

Unfortunately, in many tutorials, books and other resources about Java, the terms "object" and "variable" are used in a sloppy way, which can lead to confusion similar to what you describe in your question.

For example, look at this line of code:

String message = "Hello World";

People will often say "I have a String object named message here with the value "Hello World". This is wrong and it makes beginners think that objects and variables are the same thing, and that objects have names.

Accurate would be: "I have a variable named message here which is a reference to a String object with the value "Hello World".

Variables and objects are not the same thing. A variable* is not an object, it's a reference to an object (it's a pointer to an object somewhere in memory).

Variables have names. In the code above, there is a variable named message. Objects do not have names.

There is also not a one-to-one correspondence between variables and objects. Multiple variables may refer to the same object.

The value null is a special value that you can assign to variables* that means "this variable refers to no object". It's just like a null pointer in languages such as C and C++.

It doesn't really make sense to talk about null being an object. It's not an object, it's a special reference value to make variables refer to "no object".

If null can be assigned to any Object type

This is why I suspect you're confused about the difference between objects and variables: you do not assign null to an object, but to a variable (of "any object type" which is the same as "of a non-primitive type").

*: we're talking about variables of non-primitive types here

For a more advanced understanding:

The value null has a bit of a bad reputation (it's the cause of many bugs). You might want to use java.util.Optional to help prevent some of the problems that null causes.

See also the null object pattern, which is a design pattern about the idea of having a special object that represents "the empty value". This might be useful in special situations.

2 of 4
5

Null could have been defined to be a unique Object with behavior very similar to null references. It could be a singleton with a unique name and otherwise uninstantiable; it could compare equal to itself with == and unequal to other non-null objects; it could have a unique class, say Null.

This is, in fact, a common design pattern known as the null object pattern.

So why isn't it that way?

  • An Object variable can refer to an object of any class. But it doesn't work the other way around. You can't stuff an Object into any variable you like. I can't write:

    String str = new Object();
    

    Null objects would still require special language support. If null were a singleton object of class Object or class Null you could assign it to an Object variable, sure, but you wouldn't be able to assign it to a String or ArrayList variable. Java would require different type safety rules, making an exception for this null object so it could be assigned to any type.

  • The null object pattern is typically class-specific. You make a null object for a specific class and then implement sensible behavior for that class's methods on the null object. For example, you could create a "null" list that behaves like an empty list, returning no elements when iterated over and returning a size of 0 when queried.

    That wouldn't work with a generic null object. A null object of class Object or class Null wouldn't have implementations of the methods available on arbitrary subclasses. If null were an object, what would happen if you called null.open("...") on a File reference or null.start() on a Thread? Those methods wouldn't be defined.

    Again, there'd have to be special language support. Maybe those calls could return null themselves? (What if they return void, though?) Or maybe they could throw NullObjectExceptions?

    At some point you're just reinventing null references.

  • Finally, the simplest answer is: it's not that way because it's another way. Null could be a reference that points nowhere, or it could be a reference to a null object. Even if both were equally good solutions it can't be both. You're asking why it's "A" instead of "B". If it were "B", somebody would be asking why it's not "A".

🌐
Java Design Patterns
java-design-patterns.com › patterns › null-object
Null Object Pattern in Java: Streamlining Error Handling with Graceful Defaults | Java Design Patterns
... Null Object pattern handles "empty" objects gracefully. ... In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral ("null") behavior.

If null were an Object, it would support the methods of java.lang.Object such as equals(). However, this is not the case - any method invocation on a null results in a NullPointerException.

And this is what the Java Language Specification has to say on this topic:

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.

I think this can be boiled down to "null is special".

Answer from Michael Borgwardt on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › check if all the variables of an object are null
Check If All the Variables of an Object Are Null | Baeldung
January 8, 2024 - In this tutorial, we’ll learn four approaches to check if all variables of an object are null. The null value in Java means the absence of a variable’s value. Technically, a variable containing null doesn’t point to any position in memory or wasn’t initialized yet. That can only occur with instance variables. Primitive variables such as int, double, and boolean can’t hold null. Checking for null variables in our programs is helpful to avoid unexpected errors like IllegalArgumentException or a NullPointerException.
🌐
Wikipedia
en.wikipedia.org › wiki › Null_object_pattern
Null object pattern - Wikipedia
October 20, 2025 - The null object design pattern, ... later in the Pattern Languages of Program Design book series as "Null Object". In most object-oriented languages, such as Java or C#, references may be null....
🌐
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.
Find elsewhere
🌐
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).
🌐
Baeldung
baeldung.com › home › java › introduction to the null object pattern
Introduction to the Null Object Pattern | Baeldung
January 8, 2024 - The intent of the Null Object Pattern is to minimize that kind of null check. Instead, we can identify the null behavior and encapsulate it in the type expected by the client code.
🌐
Educative
educative.io › answers › what-is-objectsisnull-in-java
What is Objects.isNull in Java?
The method isNull is a static method of the Objects class in java that checks whether the input object reference supplied to it is null or not.
🌐
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.
🌐
SourceMaking
sourcemaking.com › design_patterns › null_object › java › 1
Null Object Design Pattern in Java
Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), one uses an object which implements the expected interface, but whose method body is empty.
🌐
Coderanch
coderanch.com › t › 673281 › java › difference-Objects-initialization-initializing-null
What's the difference in Objects between non-initialization and initializing to null? [Solved] (Beginning Java forum at Coderanch)
Henry Wong wrote: First of all, you seem to be mixing the concepts of reference variables, and the objects that they point to. Reference variables are declared -- not objects. Objects are instantiated. And also, the null keyword, is used when the reference variable is not pointing to any object.
🌐
Delft Stack
delftstack.com › home › howto › java › java check if object is null
How to Check if an Object Is Null in Java | Delft Stack
February 12, 2024 - The java.util.Objects class, introduced in Java 7, includes utility methods for object-related operations. Among these methods is isNull(), which takes an object reference as an argument and returns true if the reference is null, and false otherwise.
🌐
Java2Blog
java2blog.com › home › core java › java basics › check if object is null in java
Check if Object Is Null in Java - Java2Blog
November 29, 2023 - The most basic and efficient way to check if an object is null is by using the == operator. ... Object myObject = null;: This line declares a variable named myObject of the type Object, which is a class in Java.
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-an-object-is-null-in-java-560011
How to Check If an Object Is Null in Java | LabEx
In Java, null is a special value that indicates that a reference variable does not point to any object. Think of a variable as a box, and an object as something you put inside the box. If the box is empty, the variable is null.
🌐
Javatpoint
javatpoint.com › facts-about-null-in-java
Facts about null in Java - Javatpoint
Facts about null in Java with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Refactoring.Guru
refactoring.guru › home › techniques › simplifying conditional expressions
Introduce Null Object
January 1, 2025 - Change the code so that it returns a null object. Find all places where the variables of the real class are compared with null. Replace these checks with a call for isNull(). If methods of the original class are run in these conditionals when a value doesn’t equal null, redefine these methods in the null class and insert the code from the else part of the condition there.