The dilemma
If a variable with null value gets used in your program causing a NullPointerException, this is clearly a situation in your program which you did not expect. You must ask yourself the question: "Did I not expect it because I didn't take into consideration the possibility of a null value or did I assume the value could never be null here?"
If the answer is the latter, the problem isn't because you didn't handle the null value. The problem happened earlier, and you're only seeing the consequence of that error on the particular line it's used. In this case, simply adding a if (variable != null) isn't going to cut it. You'll wind up skipping lines you were supposed to execute because the variable was null, and you'll ultimately hit a line further on where you again assumed it wouldn't be null.
When null should be used
As a general rule, return null only when "absent" is a possible return value. In other words, your data layer may search for a record with a specific id. If that record isn't found, you can either throw an exception or simply return null. You may do either, but I prefer not to throw exceptions in situations where the strong possibility exists. So you return null instead of a value.
The caller of this method, presumably written by you, knows the possibility exists that the record may not exist and checks for null accordingly. There is nothing wrong with this in this case, though you should handle this possibility as soon as possible as otherwise everywhere in your program you will need to deal with the possibility of a null value.
Conclusion
In other words, treat null as a legitimate value, but deal with it immediately rather than wait. Ideally in your program, you should ever only have to check if it is null once in your program and only in the place where such a null value is handled.
For every value you expect to be non-null, you need not add a check. If it is null, accept that there is an error in your program when it was instantiated. In essence, favor fail fast over fail safe.
The dilemma
If a variable with null value gets used in your program causing a NullPointerException, this is clearly a situation in your program which you did not expect. You must ask yourself the question: "Did I not expect it because I didn't take into consideration the possibility of a null value or did I assume the value could never be null here?"
If the answer is the latter, the problem isn't because you didn't handle the null value. The problem happened earlier, and you're only seeing the consequence of that error on the particular line it's used. In this case, simply adding a if (variable != null) isn't going to cut it. You'll wind up skipping lines you were supposed to execute because the variable was null, and you'll ultimately hit a line further on where you again assumed it wouldn't be null.
When null should be used
As a general rule, return null only when "absent" is a possible return value. In other words, your data layer may search for a record with a specific id. If that record isn't found, you can either throw an exception or simply return null. You may do either, but I prefer not to throw exceptions in situations where the strong possibility exists. So you return null instead of a value.
The caller of this method, presumably written by you, knows the possibility exists that the record may not exist and checks for null accordingly. There is nothing wrong with this in this case, though you should handle this possibility as soon as possible as otherwise everywhere in your program you will need to deal with the possibility of a null value.
Conclusion
In other words, treat null as a legitimate value, but deal with it immediately rather than wait. Ideally in your program, you should ever only have to check if it is null once in your program and only in the place where such a null value is handled.
For every value you expect to be non-null, you need not add a check. If it is null, accept that there is an error in your program when it was instantiated. In essence, favor fail fast over fail safe.
Deciding whether or not null is a allowed as an object value is a decision that you must make consciously for your project.
You don't have to accept a language construct just because it exists; in fact, it is often better to enforce a strict rule against any nullvalues in the entire project. If you do this, you don't need checks; if a NullPointerException ever happens, that automatically means that there is a defect in your code, and it doesn't matter whether this is signalled by a NPE or by some other sanity check mechanism.
If you can't do this, for instance because you have to interoperate with other libraries that allow null, then you do have to check for it. Even then it makes sense to keep the areas of code where null is possible small if possible. The larger the project, the more sense it makes to define an entire "anti-corruption layer" with the only purpose of preserving stricter value guarantees than is possible elsewhere.
Comparing the generated bytecodes is mostly meaningless, since most of the optimization happens in run time with the JIT compiler. I'm going to guess that in this case, either expression is equally fast. If there's any difference, it's negligible.
This is not something that you need to worry about. Look for big picture optimizations.
Don't optimize at the expense of readability if the speed (or memory/whatever the case may be) gain will be negligible. I think !=null is generally more readable, so use that.
Videos
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 ReferenceTypeAt run time, the result of the
instanceofoperator istrueif the value of the RelationalExpression is notnulland the reference could be cast to the ReferenceType without raising aClassCastException. Otherwise the result isfalse.
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. Thenullreference is the only possible value of an expression of null type. Thenullreference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend thatnullis 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-
nullreference valuex,x.equals(null)shouldreturn 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 IOExceptionReturns: A
Stringcontaining the contents of the line, not including any line-termination characters, ornullif 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
nullif this map contains no mapping for the key.If this map permits
nullvalues, then a return value ofnulldoes not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key tonull. ThecontainsKeyoperation 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
nullreference 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 anullreference, 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.
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.)
Hi Java Guru
I have see some code where it uses null != vs variable!=null. Can someone please to which should I used in what situation and what sufficient does using one over the other one makes?
Thank you
public static <T> T ifNull(T toCheck, T ifNull) {
if (toCheck == null) {
return ifNull;
}
return toCheck;
}
All in all to avoid statement
if (object != null) {
....
}
since java 7 you can use
Objectsmethods:Objects.isNull(object)
Objects.nonNull(object)
Objects.requireNonNull(object)
Objects.equals(object1, object2)
since java 8 you can use Optional class (when to use)
object.ifPresent(obj -> ...); java 8
object.ifPresentOrElse(obj -> ..., () -> ...); java 9
rely on method contract (JSR 305) and use Find Bugs. Mark your code with annotations
@javax.annotation.Nullableand@javax.annotation.Nonnnul. Also Preconditions are available.Preconditions.checkNotNull(object);
In special cases (for example for Strings and Collections) you can use apache-commons (or Google guava) utility methods:
public static boolean isEmpty(CharSequence cs) //apache CollectionUtils
public static boolean isEmpty(Collection coll) //apache StringUtils
public static boolean isEmpty(Map map) //apache MapUtils
public static boolean isNullOrEmpty(@Nullable String string) //Guava Strings
- When you need to assign default value when null use apache commons lang
public static Object defaultIfNull(Object object, Object defaultValue)