Javatpoint Logo
Javatpoint Logo

Facts about null in Java

Each and every programming language, including Java, is bounded with null. There is no programmer who didn't face any issue in the code related to null. Programmers mainly face NullPointerException when they try to perform some operations with null data. NullPointerException is a class available that belongs to java.lang package.

Before understanding the facts of null, it is required to have knowledge of Java variables. If you don't know what Java variable is, go through the following link:

Each and every developer should have knowledge about the following facts of null in Java:

Case Sensitive

In Java, we cannot write null as NULL or 0 as in C programming because null is a literal and keywords are case-sensitive in Java.

Let's take an example to understand the case-sensitive behaviour of null.

NullExample1.java

Output:

Facts about null in Java

Reference Variable Value

By default, each and every reference variable has a null value in Java. A reference variable is used to indicate and store objects/values of reference type in Java. Classes, arrays, enumerations, and interfaces, etc., are some reference types in Java.

So, a reference type stores a null value if no object is passed to a reference type.

Let's take an example to understand how a reference variable works for null value:

NullExample2.java

Output:

Facts about null in Java

Null Type

In Java, null is neither an Object nor a type. It is a special value that we can assign to any reference type variable. We can cast null into any type in which we want, such as string, int, double, etc.

Let's take an example to understand how we can assign null values to any reference type.

NullExample3.java

Output:

Facts about null in Java

Autoboxing and Unboxing

Autoboxing and Unboxing are the two most important operations which we perform in Java. The compiler throws NullPointerException when we assign a null value to any primitive boxed data type while performing the operations.

Let's take an example to understand autoboxing and the unboxing fact of null.

NullExample4.java

Output:

Facts about null in Java

The instanceof Operator

In order to check whether an object is an instance of the specified type or not, we use the instanceof operator. The instanceof operator returns true when the value of the expression is not null at run time. It plays an important role in typecasting checks.

Let's take an example to understand the instanceof operator:

NullExample5.java

Output:

Facts about null in Java

Static Vs. Non-static Methods

We cannot call a non-static method on a reference variable with a null value. If we call it, it will throw NullPointerException, but we can call the static method with reference variables with null values. Since, static methods are bonded using static binding, they won't throw Null pointer Exception.

Let's take an example to understand the fact of null:

NullExample6.java

Output:

Facts about null in Java

== and != Operators

In Java, these two operators are allowed with null. Both the operators are useful in checking null with objects in Java.

Let's take an example to understand how these two operators work with null.

NullExample7.java

Output:

Facts about null in Java

Each and every Java developer should have knowledge of all the above-discussed facts of null.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA