×

Java Null Keyword

Null is a term that is only used for literal values in Java. Although it appears to be a term, it is a literal opposite of true and false. Java's use of null is somewhat infamous. Perhaps encountering the dreaded NullPointerException is a rite of passage for all Java developers. Knowing how Null functions will enable you to use it appropriately and prevent problems.

When you want to indicate the lack of a value, you can always refer to the special constant null, which is a literal in the Java language. A typical misunderstanding that novices to the Java language must contend with is that it is neither an object nor a type.

Like how there is a default value for basic types, reference types have a default value of null (such as 0 for integers and false for Booleans). Any uninitialized reference variable, including instance variables and static variables, has a default value of Null (although you will still receive a compiler warning for uninitialized local variables).To indicate the lack of anything, the concept of null was developed. In Java, you must specify the type of data that a variable is expected to hold when you define it. Variables can be thought of as containers that hold values of either of the two main kinds of categories:

  1. The programming language offers primitives as specified data types of the underlying value are directly present in the variable when you declare it as a primitive type (like int or float).
  2. References are pointers pointing to the values being represented. When a variable is specified as a reference type, the address that leads to the value rather than the actual value is stored. Reference types include things like classes, arrays, and strings.

Important points to remember about null:

  • Generally null is a case sensitive one dependent on every individual alphabet.
  • Null keyword is the reference variable's value.
  • A NullPointerException is caused by attempting to access a null reference.
  • Null is not permitted to be passed as a value when calling any primitive data type method.
  • calling a variable that's not initialised
  • utilising a null object to access or alter a data field or member
  • supplying a method with a null object as an argument
  • using a null object to call a method
  • Syncing an empty object
  • launching a blank object
  • Using a Java array to treat a null object

Examples of Java Null keyword

Example 1:

It is a Simple illustration of showing the reference variable's default value.

public class NullEx 1 {  
    static NullEx1 bj;
    public static void main (String [] s) {  
        System.out.println(bj);
    } 
}  

Output:

Null

Example 2:

 It is a case study to show how null can be passed to object reference variables.

public class NullEx2 {  
    public static void main (String [] s) {  
        NullEx2 bj = null;
        System.out.println(bj);
    }  
}  

Output:

null

Example 3:

 An example to illustration of showing the default String value.

public class NullEx3 { 
String raj;
    public static void main (String [] s) {  
        NullEx3 bj=new NullEx3();
        System.out.println(bj.raj);
    }  


}  
</pre></div>
<p><strong>Output:</strong></p>
<div class="codeblock3"><pre>
null  
</pre></div>
<h2 class="h3">Example 4</h2>
<p>Let's see an example to return null from a method. </p>
<div class="codeblock"><textarea name="code" class="java">
public class NullEx4 {  
    String disp ()  
    {  
        return null;
    }  
    public static void main (String [] s) {  
    NullEx4 bj=new NullEx4();
        System.out.println(obj. disp ());
    }  
}  

Output:

Null

Example 4:

Program for illustration of how to give the String variable null.

public class NullEx5 {  
    static String raj=null;
    public static void main (String [] s) {  
    if(raj==null)  
    {  
        System.out.println("its value is null");
    }  
    else  
    {  
        System.out.println("its value is not null");
    }  
}  
}  

Output:

Its value is null

Example 5:

Reply with empty collections rather than null

It's recommended to return empty collections rather than null values.

public class empty Str 5 {
     private static List<Integer> num = null;


     public static List<Integer>getList () {
          if (num == null)
               return Collections.emptyList();
          else
               return num;
     }
}

Related Topics

Java vs Node.js

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

Java time local date

Java: Java is one of the programming language which is object oriented. It consists of many features such as robust, simple, architecture neutral, dynamic, distributed, multi threaded, portable etc. The main feature...

3 minutes read.

Java Error Stack Trace

The stack trace in Java is an array of stacks.The stack trace reveals the console's location of an exception or error by gathering data from all program methods. The JVM...

3 minutes read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.