Unknown and Interesting facts about ‘null’ in Java

It’s very essential to have a good knowledge of ‘null’ literal.

SUMIT SHARMA
TheLeanProgrammer
4 min readFeb 12, 2021

--

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.

Unknown and Interesting facts about ‘null’ in Java

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

What is wrong with null?

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

Two types of variable declarations in Java

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.

Why we need null in Java?

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.

2. Null is the default value of all reference variables in Java.

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.

4. Null objects cannot be unboxed by the compiler as the NullPointerException will be thrown.

5. If the value is null, the result of the instanceof operator would be false.

6. Static methods can be called with a reference of the null type, but non-static methods can’t.

7. == and != operators can be used with null types in Java, which is a very important feature.

Null is important in programming languages

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

--

--

SUMIT SHARMA
TheLeanProgrammer

Software Development Engineer, Stock Market Analyst, Fitness Coach, Video Editor, Freelancer