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.
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:
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.