Java and null, both words are sharing a strong bond. Whoever is working on Java once he/she get trouble with NullPointerException. So null is very much important concept in Java.
So, what is null?
# We can define ‘null’ as absence of something.
# It is a reserved keyword in Java.
# It is case sensitive. So, we cannot write ‘Null’ / ‘NULL’ instead of ‘null’, otherwise the compile time error will be thrown like below.
# ‘null’ is default value of any kind of reference type variables which is not initialized at the time of declaration. But in case of method level reference type variables (local reference type variables), null is not set to them by default. That time we need to declare that variable ‘null’ explicitly at the declaration time if no value is specified, otherwise it will give compile time error when it will be accessed.
** ‘null’ cannot be assigned to any primitive type variables. If ‘null’ will be tried to assign to a primitive variable it will throw NullPointerException.
# ‘null’ is not a type or not an object. It is just a special value. So when ‘instanceof’ is applied to any reference variable of a particular object, which is set to null, returns false. But when that reference variable will be initialized and that time ‘instanceof’ returns true.
What is NullPointerException (NPE)?
NullPointerException is a runtime exception. This exception is thrown when program
attempts to use an object reference that has null value. As it is unchecked exception, so the Javac compiler does not force you to use a try-catch block to handle it appropriately.
This exception class is under java.lang package.
In which scenario NullPointerException is thrown and how to avoid it?
# Invoking static and non-static method through a null reference type.
A non-static method should not be invoked with a reference variable which is set to null, as it will throw a NullPointerException. But in this case a static method or variable can be invoked with the reference variable which is set to null. Because the static fields and methods are not related with the objects.
# Comparison String variable with literals
When invoking a method from String variable (which might be null), then invoking that method from the literal (the value of the String). This is very useful concept for checking equality of two String value.
# Using Equals (==) and Not Equals (! =) Operator to compare two objects
These operators are allowing null value for comparison. So, when null with objects are needed to check or compare these operators are very useful for that.
Common use case of these operators is like when we need to perform a check for null value of a method argument or return value of method.
# Use String.valueOf() method instead of toString()
When there is a requirement to print String representation of an object, use String.valueOf() method, which does not throw any exceptions and prints “null” in case the function’s argument equals to null. But toString method will throw NullPointerException if the reference equals to null.
# Use Ternary Operator
Ternary Operator can be used to handle the NullPointerException. Here if name is null then string variable ‘str’ will be set to blank, other wise str will be assigned to name.
**Difference between “”(blank) and ‘null’ Here blank means an empty string that means if we perform length() method on string variable ‘str’ it will return 0. But in case of null, it will throw NullPointerException.
When do we need the null value?
It is true that, NullPointerException / ‘null’ gives developers a lot of trouble. But to achieve some design pattern null is very much useful, such as Singleton pattern. This pattern ensures that only one instance of a class is created and aims for providing a global point of access to the object.
To achieve that pattern, we need to declare all the constructors of the class as private and then provide one public method which will return the single instance of that class.
In below example how many object will be created all will return the same ID, that means every time invoking of getInstance() method, will return the same object.
That’s it. As null creates many troubles to the developers, so it is better to handle properly to avoid the NullPointerException in your program. So check whether all the objects are initialized properly, before you use them or put a programmatically ‘null’ check on them.
Thank you for reading this blog.