×

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 Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Java String hashCode() method

Java String hashCode() method returns hash code for current String. hash code for string object is computed as s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1] Using int...

2 minutes read.

Java inheritance with Example

Java inheritance Java inheritance is a mechanism in which a child object acquires all the properties and behaviors of a parent object. It helps in reusing the code and establishes...

7 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Lambda expressions in Java

A brief introduction to Lambda expression in java In this topic, we will discuss the lambda expression in java. A lambda expression in Java is an enhanced version of an anonymous...

13 minutes read.

Time Class Operation in Java

The Java SQL package includes the time class. This class is merely a lightweight wrapper for java. util. THANKS TO THE recognize BC API can recognize this as a SQL...

3 minutes read.

Jdoodle Java

Everything is instantaneous and fast-paced in the modern world. Online compilers available via the internet are immensely helpful for programmers seeking to learn a new programming language but lacking the...

4 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.