Interesting facts about Null in Java

Interesting facts about Null in Java
Interesting facts about Null in Java

Null is definitely a very interesting topic to get to know. Not only in Java, all other programming languages like C++, Node.js, have an interesting fact about null.

So, this null is a special value in Java that can be assigned to any reference object. This null basically indicates that no value is assigned to the reference variable.

Introduction:
Let’s first discuss what exactly null in java. How java uses variables and stores null? So, in java and all other languages also, variables are used to store values, this is a kind of box that store’s values. It is needed to set the type of a variable at the time of declaration.

Java has majorly two types of variables declaration.


  • Primitive
  • Reference

Example:
String x = null; String x=”abc”;

So when String x = null,
There is no memory that is allocated to variable x.
When we have null then this is just an indication that this variable is not referring to any object.
Also, JVM takes it as all zeroes only.

Almost all the programmers face null as trouble in their life. Before going to detail about some interesting facts about null in Java. Let’s know from which class or package it is associated with. The java.lang package has java.lang.NullPointerException and null are associated with this class only.

Interesting facts about null:

As we discussed some basics about null in java. Let’s discuss some points where null seems quite interesting.

  1. Default value of any reference variable is null always.

For Example:
public class Test{
private static Object var;
public static void main(String args[]){
System.out.println(“The default value of var is : ” + var);
}
}
Output:
The default value of var is: null

So, the default value is always null for the reference variable.

Null is case sensitive. NULL and null both are different in java.

For example:
public class Test
{
public static void main (String …args)
{
Object var1 = NULL;
Object var2 = null;
}
}
Output:
Test.java:6: error: cannot find symbol
Object var1 = NULL;
^
symbol: variable NULL
location: class Test
1 error

So, here null and NULL both are different. Java could not recognise NULL.

In the case of Operator:
There is a special operator that Java has which is an instance of an operator. Basically, this operator checks whether a given object is an instance of the specified class or it may interface. The return type is boolean of instance operator. In Java, an instance of operator gives true only when the expression doesn’t have null in
runtime. In case if the expression is null then it returns false only.

For Example:
public class CheckInstanceofOperator
{
public static void main (String[] args)
{
Integer var1 = null;
Integer var2 = 10;
System.out.println(var1 instanceof Integer);
System.out.println(var2 instanceof Integer);
}
}

Output:
false
true

Operator allowed with null
In Java, null allows == and != operator .It has a great advantage because it helps in checking with objects in Java.

For Example:
public class OperatorTestInNull
{
public static void main(String args[])
{
System.out.println(null!=null);
System.out.println(null==null);
}
}
Output:
false
true

Types of null that Java has:
Well, null is something that can be assigned to anyone whether it is a String, Double, Integer etc.

  • It can be assigned to Integer. For Example: Integer var =null;
  • It can be assigned to String as well. For Example String var =null;
  • It can be assigned to Double as well. For Example: double var =null;

Note, In java typecasting of null is also allowed. We can typecast null in String, Double and Integer as well.

For Example:

  • null can be typecasted to Integer. For Example: Integer var =(Integer)null;
  • null can be typecasted to String. For Example String var =(String)null;
  • null can be typecasted to Double. For Example Double var =(Double)null;

AutoBoxing
In case when we use this autoboxing and unboxing one, Java uses to throw null pointer exceptions only if a null value is assigned to a primitive data type. This can be concluded with an example, let’s see how it works.

For Example:
public class TestAutoBoxing
{
public static void main (String[] args)
{
Integer var = null;
int x = var;
}
}
Output:
Exception in thread “main” java.lang.NullPointerException
at TestAutoBoxing.main(TestAutoBoxing.java:7)

NullPointerException indeed is a Billion Dollar Mistake:

Tony Hoare was the inventor of null, he himself calls that null is something that may lead to billion-dollar mistakes. So, we have different kinds of developers, some developers just hate this null. Some developers dislike null.

In Java, we get the NullPointerException which is just because of this null only. And this NullPointerException is known as NPE, a quite famous error for Java developers. Not is Java only but in other programming languages as well this null exception is the most common one.

Different languages have different keywords for null like nil, void, null, nothing but we have the same concept as null for all these keywords. Well, null is a way to define nothing to any programming language. There comes a lot of time where we need to represent null for a programming language.

Some common uses of null:

Suppose a person X has email then we give the email like:
String email = “xyz@gmail.com”

While if he/she doesn’t have email then we write it as:
String email = null;

What is the solution for this?
Now we have discussed how important this null is and the impact it may cause to the life of a programmer. So, what can be the solutions or options to mitigate this? There can be a number of scenarios where we may need to handle null and we programmers may forget to handle null, which is a common mistake and indeed a billion dollar mistake at some point of time.

Just think of a language with a compiler which can handle null automatically. The compiler should throw a warning if we just forget to handle null. The compiler should not allow it to pass it and should throw the warning until the null is handled. Well, we can have a language and the compiler like this and null can be handled for sure. This could be the most effective way to handle null for sure.

Methods to avoid NullPointerException:
Well,there can be different ways to handle null.
Let’s discuss some of them.

  1. Using Ternary Operator: With the use of the ternary operator, null handling is quite easy. Ternary operator basically returns true when an expression is evaluated correctly.
    Example:

public class GFG
{
public static void main (String[] args)
{
String str = “HelloWorld”;
String message = (str == null) ? “” : str.substring(0,5);
System.out.println(message);
}
}
Output:
Hello

2. Comparing String With Literals: So null Pointer exception can also be avoided by calling equals on literals rather than an object.

Conclusion:
As we saw the impact of null and also on what level it can impact the life of a programmer. Now, also we discussed how important it is in a programmer’s life. Literally, it can make hell to a programmer’s life for sure.
Also, we saw what all are the most interesting facts about null. As this is quite a good issue with all the programmers. So, having such a language that can throw a warning if null is not handled can be a blessing for the programming life indeed.

To continue reading more articles, click here and visit www.codingninjas.com to explore our programming courses.

By Deepak Jain

Exit mobile version