×

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

Diffie Hellman Algorithm in Java

In this section, you will be acknowledged about Diffie Hellman algorithm clearly step wise along with an example and also an example program. Diffie Hellman Algorithm One of the most significant algorithms...

3 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

Java Byte Keyword

Byte: The Keyword Byte in Java programming language is a primitive data type.Digital content that is most used for eight bits is called as a byte.The Java Byte Keyword is used...

3 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Java Math floor() Method

The floor() method of Math class returns the largest double value that is equal to a mathematical integer and is less than or equal to the argument. Syntax: public static double floor(double...

2 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.

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Java Boolean getBoolean() Method

The getBoolean() method of Java Boolean class returns true if the specified system property is not null and is equal to the String ‘true’, else the result returned is false. Syntax public...

1 minute read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.