What is null?
Is null an instance of anything?
What set does null belong to?
How is it represented in the memory?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, there is no type which
nullis aninstanceof.15.20.2 Type Comparison Operator
instanceofThis means that for any type
EandR, for anyE o, whereo == null,o instanceof Ris alwaysfalse.JLS 4.1 The Kinds of Types and Values
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 (fromjava.lang.Object):It is also the default value (for variables that have them) for all reference types:
JLS 4.12.5 Initial Values of Variables
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
nulluntil 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:This is a very common use pattern:
nullis used to denote non-existence of an object.Here’s another usage example, this time from
java.io.BufferedReader:So here,
readLine()would returninstanceof Stringfor each line, until it finally returns anullto 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()returningnull, 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>:Here we start to see how using
nullcan complicate things. The first statement says that if the key isn’t mapped,nullis returned. The second statement says that even if the key is mapped,nullcan also be returned.In contrast,
java.util.Hashtablekeeps things simpler by not permittingnullkeys and values; itsV get(Object key), if returnsnull, unambiguously means that the key isn’t mapped.You can read through the rest of the APIs and find where and how
nullis used. Do keep in mind that they aren’t always the best practice examples.Generally speaking,
nullare used as a special value to signify:In Java? None of your concern. And it’s best kept that way.
Is
nulla good thing?This is now borderline subjective. Some people say that
nullcauses many programmer errors that could’ve been avoided. Some say that in a language that catchesNullPointerExceptionlike Java, it’s good to use it because you will fail-fast on programmer errors. Some people avoidnullby 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
nullhimself, C.A.R Hoare (of quicksort fame):The video of this presentation goes deeper; it’s a recommended watch.