Unknown and Interesting facts about ‘null’ in Java
It’s very essential to have a good knowledge of ‘null’ literal.
What is wrong with null? Some facts about ‘null’ that every Java developer should know.
Not only Java, but all the programming languages have the null concept, as it is a fundamental concept in almost all programming languages. And I can bet there could hardly be a developer or programmer who is not troubled by null, at least once in their life. So being a very interesting and known topic, it becomes very important to fully grasp the idea of null in detail.
What is wrong with null?
Null is a value that is not a value. That is the problem with null.
In short — null is a non-value value
Before starting our discussion directly about null, we have to go few steps back, to know how many types of variable declarations are there in Java. Well, we will discuss the facts about ‘null’ in detail, once we discuss variable declarations.
There are majorly two types of variable declarations —
1. Primitive Type
2. Reference Type
Which class or package does null belong to?
null is associated with java.lang.NullPointerException class, which is a part of the java.lang package.
Why we need null in Java?
When programmers do not explicitly mention the value of primitive types, the default value is stored for them based on their data type. Like uninitialized int will have 0 as default, uninitialized boolean will have false as default, and so on …
But what reference type variables will contain, if they are not explicitly referencing some object in memory?
Yes, You got that right! They will store null!
That is why we need null in Java. Null has a special purpose and meaning.
We have different kind of programmers, some hate null while other like it. So to stop hating null, if you will correctly learn its semantics and implementation, null will become your favorite irrespective of its complexity. That is why every programmer must be aware of the interesting facts about null literal to develop a feeling of love towards null.
Unknown and Interesting facts about ‘null’ in Java —
Now if we have reached this level, we are aware of the basics of null and also its importance. Now is the correct time to discuss the specifications of null literal.
1. Null is case sensitive, which means we can’t write it as NULL or 0.
// Error
Object object1 = NULL;// Runs Successfully
Object object2 = null;/* Error: Java: cannot find symbol
symbol: variable NULL */
2. Null is the default value of all reference variables in Java.
private static Object object;
public static void main(String[] args) {
System.out.println("Value is - " + object);
}// Output: Value is - null
3. Null can be assigned to any reference type and we can typecast null to any type. Null is neither an Object nor a type, it is just a special value.
// null can be assigned to String
String str = null;// to Double
Double d = null;// to Integer, and etc.
Integer i = null;
4. Null objects cannot be unboxed by the compiler as the NullPointerException will be thrown.
// This code is fine
Integer i = null;// Unboxing throws NullPointerException
int a = i;/* Exception to thread "main"
java.lang.NullPointerException */
5. If the value is null, the result of the instanceof operator would be false.
Double a = null;
Double b = 40d;// False
System.out.println(a instanceof Double);// True
System.out.println(a instanceof Double);
6. Static methods can be called with a reference of the null type, but non-static methods can’t.
public static void main(String[] args) {
Main object = null;
object.staticMethod();
object.nonStaticMethod();
}// This method will be called
private static void staticMethod() {
System.out.println("Static method will be called");
}// NullPointerException will be thrown
private static void nonStaticMethod() {
System.out.println("Non-static method can't");
}
7. == and != operators can be used with null types in Java, which is a very important feature.
Integer i = null;// False
System.out.println(i != null);// True
System.out.println(i == null);
Literally null can be a nightmare in a developer's life. I would like to know your point of view, did you face NullPointerException and what steps did you take to resolve it?
That was all! Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer